- Example //precondition: x >= 0 public void sqrt(double x) double root; if (x < 0.0) //What to do? else //compute the square root of x return root; 1 - Example //precondition: x >= 0 public void sqrt(double x) double root; if (x < 0.0) //throw an error exception throw new IllegalArgumentException( square is lower than 0 ); else //compute the square root of x return root; 2 1
This programm causes an error: public class makeexception static public void main (String[] args) int a = 100; int b = 0; int c = 0; c = a/b; Exception in thread "main java.lang.arithmeticexception: / by zero at MakeException.main(MakeException.java:15) 3 Class hierarchy Throwable unchecked checked Error Exception checked MyException unchecked Runtime Exception 4 2
Class Throwable: public String getmessage() yields an error message. public void printstacktrace() yields the function stack that is the calling hierarchie. 5 6 3
Checked and unchecked exceptions: A checked exception indicates an error case which you cannot avoid in each case. Sometimes it will occur. All subclasses of IOException are checked. In contrast to that, an unchecked exception indicates an error which you can and should avoid in any case. All subclasses of RuntimeException, for example IllegalArgumentException, NullPointerException are unchecked. When you call a method that throws a checked exception, the compiler checks that you do not ignore it. You must tell the compiler what you are going to do. In contrast to that, the compiler does not require you to keep track of unchecked exceptions. Error: A class for really bad errors, the program must terminate (for example OutOfMemoryError). 7 public void readfile(string filename) throws FileNotFoundException 8 4
public void readfile(string filename) try FileReader reader = new FileReader(filename); Scanner in = new new Scanner(reader); catch(filenotfoundexception e) //here comes the error handling 9 public void readfile(string filename) throws FileNotFoundException, 10 5
Since FileNotFoundException is a subclass of IOException, we can also write: public void readfile(string filename) throws IOException 11 The compiler can be forced to track back unchecked exceptions: public void sqrt(double x) throws IllegalArgumentException 12 6
If an error takes place, a error message called exception is thrown. An error message is represented by the class java.lang.exception (subclass of Throwable) An exception indicates an error which possibly can be handled. Handling by the catch- throw- rule: Handle the exception (error) by the current block or give it back to the calling block. The user can write an own Exception class as a subclass of Exception. The Error class represent very wrong errors. The program has to be terminated. There are checked and unchecked exceptions. The compiler has to be told what to do about the exception if it is ever thrown. 13 This programm causes an error: Which one? public class makeexception static public void main (String[] args) int[ ] arr = new int[100]; int n; for (n=0;;) arr[n++] = 0; 14 7
try String filename = ; FileReader reader = new FileReader(filename); Scanner in = new Scanner(reader); String input = in.next(); int value = Integer.parseInt(input); catch(ioexception e) e.printstacktrace(); catch(numberformatexception e) System.out.println( Input was not a number ); 15 General try Block try //instructions which possibly causes an error... catch (ExceptionType1 e1) //handle the exception of type ExceptionType1... catch (ExceptionType2 e2)...... catch (ExceptionTypeN en)... finally //limit the damage of the error... 16 8
The finally Clause The code of the finally clause is executed whenever the try block is exited in any of three ways After completing the last statement of the try block After completing the last statement of a catch clause, if this try block caught an exception When an exception was thrown in the try block not caught an exception Use it to leave the try- and- catch in a regular mode. 17 Example: FileReader reader = new FileReader(filename); Scanner in = new Scanner(reader); readdata(in); reader.close(); //may never get here What to do when of the methods before the last line throws an exception? 18 9
Place the call to close inside a finally clause: FileReader reader = new FileReader(filename); try Scanner in = new Scanner(reader); readdata(in); finally reader.close(); 19 Quality tip: Do not use catch and finally in the same try statement! try FileReader reader = new FileReader(filename); try //Read input Scanner in = new Scanner(reader); readdata(in); finally reader.close(); catch (IOException e) //Handle exception 20 10
Exception Designing your own exception types: Typially as a subclass of RuntimeException. Example: class NewException extends RuntimeException public NewException(String message) super(message);... 21 11