What is an Exception?
Types of Problems: errors and exceptions.
Throwable is the superclass for all exceptions & errors.
The Exception and Error classes are organized in a hierarchy of subclasses.
For example:
This allows the program to catch the specific ArrayIndexOutOfBoundsException, and/or any of its superclasses IndexOutOfBoundsException, RuntimeException or Exception. The more general the exception (higher in the hierarchy), the more subclass exceptions could be caught, but handling them properly would require more work.
try { // statements monitored for exceptions}catch( exception-class obj-reference ){ // statements that handle exception}/* There may be more than one catch clause if more than one exception is caught */Example:
//be sure to import java.util.* for ArrayListArrayList<String> list = new ArrayList<String>();String str; int i; boolean errorOccurred;// other code may be heredo { errorOccurred = false; try{ // code here to assign to i str=list.get(i);// might throw an exception // put code here for when there's NO exception } catch(IndexOutOfBoundsException exp){ errorOccurred=true; System.err.println("Exception: " + exp); // should do more to "handle" the exception here } // end catch exception}while( errorOccurred);// This is inside some methodint intCh;char ch;try { System.out.println("Enter a character: "); intCh = System.in.read(); // may throw IOException ch = (char) intCh; . . .}// end trycatch( IOException ex ){// may be other statements here System.err.println(ex); // calls toString on ex} // end catch IOExceptioncatch( Exception ex ){ System.err.println(ex);} // end catch Exception
finally {
// cleanup statements
}
finally { str = null; list = null; }
public abstract int read() throws IOException { . . .}
unless your method also is defined with the throws clause for the same exception(s) that the called method throws (more on throws clause on the next page)
/*Exercise 10.1 - Determine which exception will be thrown by the Scanner class' nextInt or nextDouble methods. Change a previously written Java program that reads numeric input from the user. Call the input method in a try block and catch the specific exception that the input method would throw if the user entered non-numeric input. Allow the user to input the number again if an exception is caught. Suggestion: DON'T use recursion, but put the try block and catch clause inside a loop, using a boolean variable that will be tested in the loop condition. */import java.util.Scanner;import java.util.InputMismatchException;public class Exercise_10_1_Exception{static Scanner oScanner = new Scanner(System.in);
private int i_Total = 0;
public boolean inputMethod()
{
boolean isError = false;
try // Comments: Score deduction - should put try catch inside loop
{
for(int i = 0; i < 5; i++)
{
System.out.println("Enter integer number to sum");
i_Total = i_Total + oScanner.nextInt();
}
}
catch(InputMismatchException ex)
{
isError = true;
i_Total = 0;
}
return isError;
}
public static void main( String[] args )
{
Exercise_10_1_Exception oException = new Exercise_10_1_Exception();
// Catch java.util.InputMismatchException for oScanner.nextint()
boolean isException = oException.inputMethod();
if(!isException)
System.out.println("Total = " + oException.i_Total);
else
System.out.println("Exception: Invalid integer");
} // end main
} // end class Exercise_10_1_Exceptionfor example,
public void someMethod( int i ) throws IOException, MyException{. . . } // MyException is a subclass of Exception that you writethrow(exObj);
if( myExceptOccurred ){ MyException ex = new MyException("MyException Occurred"); throw( ex );}NOTE ABOUT WHERE TO THROW AND CATCH EXCEPTIONS: The reason for throwing an exception is to allow another method handle the exception, different from the method where the error occurred. Therefore, it's a bad idea to throw an exception and include a corresponding catch clause in the same method as the throw statement. If you want to handle an error inside the same method, just use if-else statements.
/*Exercise 10.2: Change any class from a previous exercise or programming homework assignment to throw an exception
if the input or parameter is out of range. Write a driver application program that catches that exception,
but continues (after handling the exception, may loop). Make sure the catch clause is not in the same method
as the throw statement.
*/// Note: Modified exercise 2.4 to get cube using square function (Not a good example but just to demonstrate concept)import java.io.*;import java.util.Scanner;public class Exercise_10_2_ThrowException{static Scanner oScanner = new Scanner(System.in);
public static void main(String[] args)
{
int i_num;
int i_cube;
for(int i = 0; i < 5; i++)
{
System.out.println("Enter number to get cube (-500 to 500 range)");
i_num = oScanner.nextInt();
i_cube = getCube(i_num);
if(i_cube == -1000)
System.out.println("Error in getCube(), continue with next number.");
}
System.out.println("Main Ends");
}
public static int getCube(int i)
{
int i_cube = 0;
try
{
i_cube = i * getSquare(i);
System.out.println("Cube: " + i_cube);
}
catch(IOException e)
{
System.err.println("Error: " + e);
i_cube = -1000; // sample indicator
}
return i_cube;
}
public static int getSquare(int i) throws IOException
{
IOException oIOException = new IOException("Number is out of range");
if(i >= -500 && i <= 500)
return i * i;
else
throw(oIOException);
}
}class java.lang.Throwable (implements java.io.Serializable) class java.lang.Error class java.lang.LinkageError class java.lang.ClassCircularityError class java.lang.ClassFormatError class java.lang.UnsupportedClassVersionError class java.lang.ExceptionInInitializerError class java.lang.IncompatibleClassChangeError class java.lang.AbstractMethodError class java.lang.IllegalAccessError class java.lang.InstantiationError class java.lang.NoSuchFieldError class java.lang.NoSuchMethodError class java.lang.NoClassDefFoundError class java.lang.UnsatisfiedLinkError class java.lang.VerifyError class java.lang.ThreadDeath class java.lang.VirtualMachineError class java.lang.InternalError class java.lang.OutOfMemoryError class java.lang.StackOverflowError class java.lang.UnknownError class java.awt.AWTError class java.lang.Exception class java.lang.ClassNotFoundException class java.lang.CloneNotSupportedException class java.lang.IllegalAccessException class java.lang.InstantiationException class java.lang.InterruptedException class java.lang.NoSuchFieldException class java.lang.NoSuchMethodException class java.lang.RuntimeException class java.lang.ArithmeticException class java.lang.ArrayStoreException class java.lang.ClassCastException class java.lang.IllegalArgumentException class java.lang.IllegalThreadStateException class java.lang.NumberFormatException class java.lang.IllegalMonitorStateException class java.lang.IllegalStateException class java.awt.IllegalComponentStateException class java.lang.IndexOutOfBoundsException class java.lang.ArrayIndexOutOfBoundsException class java.lang.StringIndexOutOfBoundsException class java.lang.NegativeArraySizeException class java.lang.NullPointerException class java.lang.SecurityException class java.lang.UnsupportedOperationException class java.awt.AWTException class java.io.IOException class java.io.CharConversionException class java.io.EOFException class java.io.FileNotFoundException class java.io.InterruptedIOException class java.io.ObjectStreamException class java.io.InvalidClassException class java.io.InvalidObjectException class java.io.NotActiveException class java.io.NotSerializableException class java.io.OptionalDataException class java.io.StreamCorruptedException class java.io.WriteAbortedException class java.io.SyncFailedException class java.io.UnsupportedEncodingExceptionclass java.io.UTFDataFormatExceptionimport java.awt.*;import java.awt.event.*;import javax.swing.*;public class ExceptionHandlingExample extends JFrame implements ActionListener{ private String str; private JTextField tf1, tf2; private int sum=0; public ExceptionHandlingExample(){ super("Exception Handling Example"); setLayout(new FlowLayout()); str = ""; tf1= new JTextField(10); tf1.addActionListener(this); tf1.setVisible(true); tf1.setEditable(true);// default tf2=new JTextField(30); tf2.setEditable(false); add(new JLabel("Enter a number and press [Enter] ")); add(tf1); add(tf2); } // end constructor public void actionPerformed(ActionEvent e){ int inputInt=0; try { inputInt = Integer.parseInt(tf1.getText()); sum += inputInt; tf2.setText("Sum is now "+ sum); } // end try catch( NumberFormatException nfe ){ JOptionPane.showMessageDialog(null, "Must enter digits only!", "Input Error", JOptionPane.ERROR_MESSAGE); } // end catch tf1.setText(""); } public static void main( String[] args ){ ExceptionHandlingExample fp =new ExceptionHandlingExample(); fp.setSize(400, 200); fp.setVisible(true); fp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } // end main} // end class ExceptionHandlingExampleimport java.io.*;public class NegativeNumberException extends IOException{public NegativeNumberException(double irritant){ super("non-negative number expected, not " + irritant);}public NegativeNumberException(){ super("non-negative number expected");}} // end NegativeNumberException class// in a separate file:public class ExceptionDemo{public static double squareRoot(double x) throws NegativeNumberException{System.out.println("Entering squareRoot()");if (x < 0){ NegativeNumberException exp = new NegativeNumberException(x); throw(exp);}System.out.println("Exiting squareRoot()");return Math.sqrt(x);}public static double cubeOfSquareRoot(double x) throws NegativeNumberException{System.out.println("Entering cubeOfSquareRoot()");double temp = squareRoot(x); // may implicitly throw exception hereSystem.out.println("Exiting cubeOfSquareRoot()");return temp * temp * temp;}public static void displayCubeOfSquareRoot(double x){double result = 0;System.out.println( "Entering displayCubeOfSquareRoot()");try{ result = cubeOfSquareRoot(x); System.out.println("result = " + result); }catch(NegativeNumberException e){ System.err.println("Error: " + e); // more steps here?}System.out.println( "Exiting displayCubeOfSquareRoot()");}public static void main(String[] args){System.out.println("Entering main()");System.out.println( "calling displayCubeOfSquareRoot(16)");displayCubeOfSquareRoot(16);System.out.println ("calling displayCubeOfSquareRoot(-16)");displayCubeOfSquareRoot(-16); // troubleSystem.out.println("Exiting main()");} // end main} // ExceptionDemo