Presentation is loading. Please wait.

Presentation is loading. Please wait.

EXCEPTIONS There's an exception to every rule.. 2 Introduction: Methods  The signature of a method includes  access control modifier  return type 

Similar presentations


Presentation on theme: "EXCEPTIONS There's an exception to every rule.. 2 Introduction: Methods  The signature of a method includes  access control modifier  return type "— Presentation transcript:

1 EXCEPTIONS There's an exception to every rule.

2 2 Introduction: Methods  The signature of a method includes  access control modifier  return type  name of the method  parameter list  There is also an implied 'contract' for what the method should do when used.  This contract can be formalized as pre/post conditions  What should happen if the method can't fulfill the contract? Perhaps the pre-conditions aren't met?

3 3 Introduction: Example of 'error'  For instance, assume that a function with the signature “public double squareRoot(double value)” is written.  What would the contract be in general?  What would you think the pre/post conditions to be?  What would the value of “root” be in the statement double root = squareRoot(-1.0);  If a method may fail to work as expected due to some error (pre-condition violated for example), a method should generate an exception.  An 'exception' should generally be understood as an 'error'. Exceptions are used to:  Separate error-free code from erroneous code  Force programmers to specify and know what errors might arise in their code

4 4 Overview of the Exception class  Exceptions are objects!  Exception is the root class of all exceptions  An exception object should generally specify the precise reason/kind of error that occurred  Some common predefined exceptions:  ArrayIndexOutOfBoundsException  IOException  NullPointerException  FileNotFoundException  Many pre-defined methods throw exceptions  the documentation or source code will tell you when  the exceptions thrown are also pre-defined

5 5 Exception terminology  Throwing an exception  An error has occurred and the method cannot continue ‘normally’.  An Exception object is created and an error-handling process begins.  The error handling process 'jumps' to some line of code that is meant to take care of the error.  Handling an exception  Code that is meant to take care of an error.  This is also called catching an exception.  Java Syntax  To create and exception, create an Exception object (usually a subclass)  To throw an exception, use the throws clause.  Place code that may cause an error in a try block  Exception handling code is placed in a catch block

6 6 try-throw-catch syntax try { if(some_error_occurs) { throw new Exception(“An error has occurred"); } } catch(Exception e) { }

7 7 try-throw-catch flow of control  Details of try block:  Statements execute until a throw statement is executed.  If a throw statement is executed in the try block all code in the try block is done (even if not yet executed) Code jumps to the catch block associated with the try block  Details of the catch block:  Executes only if an exception is thrown from the associated try block.  Normal execution resumes after the code in the catch block completes

8 8 User enters 321 Example 1A Checking for valid data int age; try { System.out.print(“Enter your age: “); age = Keyboard.readInt(); if(age 120) { throw new Exception(“Invalid age: “ + age); } System.out.println(“Your age is ” + age); } catch (Exception e) { System.out.println(e.getMessage()); } System.out.println(“Done.”);

9 9 evaluates to true (an error is detected) Example 1A int age; try { System.out.print(“Enter your age: “); age = Keyboard.readInt(); if(age 120) { throw new Exception(“Invalid age: “ + age); } System.out.println(“Your age is ” + age); } catch (Exception e) { System.out.println(e.getMessage()); } System.out.println(“Done.”);

10 10 create an Exception object Example 1A int age; try { System.out.print(“Enter your age: “); age = Keyboard.readInt(); if(age 120) { throw new Exception(“Invalid age: “ + age); } System.out.println(“Your age is ” + age); } catch (Exception e) { System.out.println(e.getMessage()); } System.out.println(“Done.”);

11 11 throw the Exception object Example 1A int age; try { System.out.print(“Enter your age: “); age = Keyboard.readInt(); if(age 120) { throw new Exception(“Invalid age: “ + age); } System.out.println(“Your age is ” + age); } catch (Exception e) { System.out.println(e.getMessage()); } System.out.println(“Done.”);

12 12 catch the exception object Example 1A int age; try { System.out.print(“Enter your age: “); age = Keyboard.readInt(); if(age 120) { throw new Exception(“Invalid age: “ + age); } System.out.println(“Your age is ” + age); } catch (Exception e) { System.out.println(e.getMessage()); } System.out.println(“Done.”);

13 13 the exception object is named “e” in this block Example 1A int age; try { System.out.print(“Enter your age: “); age = Keyboard.readInt(); if(age 120) { throw new Exception(“Invalid age: “ + age); } System.out.println(“Your age is ” + age); } catch (Exception e) { System.out.println(e.getMessage()); } System.out.println(“Done.”);

14 14 execute the “error handling” code Example 1A int age; try { System.out.print(“Enter your age: “); age = Keyboard.readInt(); if(age 120) { throw new Exception(“Invalid age: “ + age); } System.out.println(“Your age is ” + age); } catch (Exception e) { System.out.println(e.getMessage()); } System.out.println(“Done.”);

15 15 continue normally after the catch block Example 1A int age; try { System.out.print(“Enter your age: “); age = Keyboard.readInt(); if(age 120) { throw new Exception(“Invalid age: “ + age); } System.out.println(“Your age is ” + age); } catch (Exception e) { System.out.println(e.getMessage()); } System.out.println(“Done.”);

16 16 User enters 32 Example 1B Checking for valid data int age; try { System.out.print(“Enter your age: “); age = Keyboard.readInt(); if(age 120) { throw new Exception(“Invalid age: “ + age); } System.out.println(“Your age is ” + age); } catch (Exception e) { System.out.println(e.getMessage()); } System.out.println(“Done.”);

17 17 evaluates to false (no error is detected) Example 1B int age; try { System.out.print(“Enter your age: “); age = Keyboard.readInt(); if(age 120) { throw new Exception(“Invalid age: “ + age); } System.out.println(“Your age is ” + age); } catch (Exception e) { System.out.println(e.getMessage()); } System.out.println(“Done.”);

18 18 continue execution normally since the “throw” statement is not executed Example 1B int age; try { System.out.print(“Enter your age: “); age = Keyboard.readInt(); if(age 120) { throw new Exception(“Invalid age: “ + age); } System.out.println(“Your age is ” + age); } catch (Exception e) { System.out.println(e.getMessage()); } System.out.println(“Done.”);

19 19 continue execution normally since the “throw” statement is not executed Example 1B int age; try { System.out.print(“Enter your age: “); age = Keyboard.readInt(); if(age 120) { throw new Exception(“Invalid age: “ + age); } System.out.println(“Your age is ” + age); } catch (Exception e) { System.out.println(e.getMessage()); } System.out.println(“Done.”);

20 20 Example 2 int a[] = new int[2]; try { for(int i=0; i<4; i++) { a[i] = 0; } System.out.println(“Done with the try block”); } catch (ArrayIndexOutOfBoundsException e) { System.out.println(e.getMessage()); } System.out.println(“Array a is initialized.”);

21 21 when i = 2 this causes an ArrayIndexOutOfBoundsException Example 2 int a[] = new int[2]; try { for(int i=0; i<4; i++) { a[i] = 0; } System.out.println(“Done with the try block”); } catch (ArrayIndexOutOfBoundsException e) { System.out.println(e.getMessage()); } System.out.println(“Array a is initialized.”);

22 22 this catch block handles the exception Example 2 int a[] = new int[2]; try { for(int i=0; i<4; i++) { a[i] = 0; } System.out.println(“Done with the try block”); } catch (ArrayIndexOutOfBoundsException e) { System.out.println(e.getMessage()); } System.out.println(“Array a is initialized.”);

23 23 Exception Class Hierarchy Checked (you must follow all of the rules) OtherThanRuntimeException Not Checked (you can ignore some of the rules) RuntimeException

24 24 Rolling your own Exception classes  Must be derived from some pre-defined exception class  Often the only methods you need to define are the constructors  Include a constructor that takes a String message argument  Include a default constructor public class YouEnteredWrongDataException extends Exception { public YouEnteredWrongDataException() { } public YouEnteredWrongDataException(String message) { super(message); } public class YouEnteredWrongDataException extends Exception { public YouEnteredWrongDataException() { } public YouEnteredWrongDataException(String message) { super(message); }

25 25 Example of User Defined Exceptions public int getAge() throws YouEnteredWrongDataException { int result = Keyboard.readInt(); if(result 100) throw new YouEnteredWrongDataException(); return result; } public int getAge() throws YouEnteredWrongDataException { int result = Keyboard.readInt(); if(result 100) throw new YouEnteredWrongDataException(); return result; } Methods can throw exceptions and not catch them. This is the most common (and usually the best) way to write code!

26 26 Catching an exception in a method that doesn’t throw it!  When defining a method you must include a throws -clause if your method could throw an exception.  This tells the caller that it must deal with this possible error  The caller may either defer or handle Defer : simply not handle it (put in throws-clause) Handle : write a try-catch block This tells other methods: "If you call me, you must handle any exceptions that I throw."

27 27 Deferring methodA throws MyException but doesn’t catch it. Must be declared in the throws clause. methodB, which calls methodA, doesn’t catch MyException exceptions. Must be declared in the throws clause. public void methodA() throws MyException { … } public void methodA() throws MyException { … } public void methodB() throws AnException { … methodA(); … } public void methodB() throws AnException { … methodA(); … }

28 28 Handling the exception methodA throws AnException but doesn’t catch it. Must be declared in the throws clause. methodB, which calls methodA, catches MyException exceptions: public void methodA() throws MyException { …… } public void methodA() throws MyException { …… } public void methodB() { … try { MethodA(); … } catch(MyException e) { } … } public void methodB() { … try { MethodA(); … } catch(MyException e) { } … }

29 29 Uncaught exceptions  If an exception is not caught in the method that throws it or any of its calling methods, either:  the program ends  the program becomes unstable  Your main method should NEVER have a throws clause! “RunTimeExceptions" do not need a catch block or throws - clause…you can break the rules since these exceptions are not checked.

30 30 Multiple exceptions and catch blocks in a method  Methods can throw more than one exception  Try blocks can have more than one catch block associated with them  The catch blocks immediately following the try block are searched in sequence for a catcher of the appropriate type  the first catch block that handles the exception is the only one that executes  Specific exceptions are typically derived from more general types  So put the catch blocks for the more specific exceptions early and the more general ones later

31 31 Multiple Catch Blocks l If the call to “someMethod” throws an exception then: »If the exception is of type MyException1 then execute only the first catch block »Else if the exception is of type MyException2 then execute only the second catch block »Else if the exception is of type MyException3 then execute only the third catch block l If the call to “someMethod” throws an exception then: »If the exception is of type MyException1 then execute only the first catch block »Else if the exception is of type MyException2 then execute only the second catch block »Else if the exception is of type MyException3 then execute only the third catch block try { … someMethod(); // this may throw an exception … } catch(MyException1 me1) { } catch(MyException2 me2) { } catch(MyException3 me3) { }

32 32 Multiple Catch Block Example Sample thing = new Sample(); try { System.out.println(“about to do something”); thing.doSomething(); // this method may throw an exception System.out.println(“finished doing something”); } catch(BadThing1Exception bt1e) { System.out.println(“Bad thing 1 occurred”); } catch(BadThing2Exception bt2e) { System.out.println(“Bad thing 2 occurred”); } catch(BadThing3Exception bt3e) { System.out.println(“Bad thing 3 occurred”); } System.out.println(“continuing on with the program…”); Sample thing = new Sample(); try { System.out.println(“about to do something”); thing.doSomething(); // this method may throw an exception System.out.println(“finished doing something”); } catch(BadThing1Exception bt1e) { System.out.println(“Bad thing 1 occurred”); } catch(BadThing2Exception bt2e) { System.out.println(“Bad thing 2 occurred”); } catch(BadThing3Exception bt3e) { System.out.println(“Bad thing 3 occurred”); } System.out.println(“continuing on with the program…”); Object Throwable Exception BadThing1ExceptionBadThing2ExceptionBadThing3Exception Assume the class hierarchy shown below

33 33 Multiple Catch Block Example Sample thing = new Sample(); try { System.out.println(“about to do something”); thing.doSomething(); // this method may throw an exception System.out.println(“finished doing something”); } catch(BadThing1Exception bt1e) { System.out.println(“Bad thing 1 occurred”); } catch(BadThing2Exception bt2e) { System.out.println(“Bad thing 2 occurred”); } catch(BadThing3Exception bt3e) { System.out.println(“Bad thing 3 occurred”); } System.out.println(“continuing on with the program…”); Sample thing = new Sample(); try { System.out.println(“about to do something”); thing.doSomething(); // this method may throw an exception System.out.println(“finished doing something”); } catch(BadThing1Exception bt1e) { System.out.println(“Bad thing 1 occurred”); } catch(BadThing2Exception bt2e) { System.out.println(“Bad thing 2 occurred”); } catch(BadThing3Exception bt3e) { System.out.println(“Bad thing 3 occurred”); } System.out.println(“continuing on with the program…”); Assume the class hierarchy shown below Object Throwable Exception BadThing1Exception BadThing2Exception BadThing3Exception

34 34 Multiple Catch Block Example Sample thing = new Sample(); try { System.out.println(“about to do something”); thing.doSomething(); // this method may throw an exception System.out.println(“finished doing something”); } catch(BadThing1Exception bt1e) { System.out.println(“Bad thing 1 occurred”); } catch(BadThing2Exception bt2e) { System.out.println(“Bad thing 2 occurred”); } catch(BadThing3Exception bt3e) { System.out.println(“Bad thing 3 occurred”); } System.out.println(“continuing on with the program…”); Sample thing = new Sample(); try { System.out.println(“about to do something”); thing.doSomething(); // this method may throw an exception System.out.println(“finished doing something”); } catch(BadThing1Exception bt1e) { System.out.println(“Bad thing 1 occurred”); } catch(BadThing2Exception bt2e) { System.out.println(“Bad thing 2 occurred”); } catch(BadThing3Exception bt3e) { System.out.println(“Bad thing 3 occurred”); } System.out.println(“continuing on with the program…”); Assume the class hierarchy shown below Object Throwable Exception BadThing3Exception BadThing2Exception BadThing1Exception

35 35 Multiple Catch Block Example BufferedReader fin = null; try { System.out.println(“about to open a file”); // this method may throw an Exception fin = new BufferedReader(new FileReader(file)); System.out.println(“finished opening the file”); } catch(FileNotFoundException fnf) { System.out.println(“File “ + file + “ doesn’t exist”); } catch(IOException io) { System.out.println(“Error with file “ + file + “.”); } System.out.println(“continuing on with the program…”); BufferedReader fin = null; try { System.out.println(“about to open a file”); // this method may throw an Exception fin = new BufferedReader(new FileReader(file)); System.out.println(“finished opening the file”); } catch(FileNotFoundException fnf) { System.out.println(“File “ + file + “ doesn’t exist”); } catch(IOException io) { System.out.println(“Error with file “ + file + “.”); } System.out.println(“continuing on with the program…”); Assume the class hierarchy shown below Object Throwable Exception IOException FileNotFoundException

36 36 Example Program  Write a class that represents a rational number  a number which is expressed as the quotient of two integers.  Must throw exceptions when reasonable  Write a program that demonstrates the use of this class Rational number ADT What data? What operations? What pre-conditions to the operations? What errors could occur in these operations? Rational number ADT What data? What operations? What pre-conditions to the operations? What errors could occur in these operations?

37 37 Example Program Using Exceptions class DivideByZeroException extends Exception { DivideByZeroException(String msg) { super(msg); } DivideByZeroException() {} } class DivideByZeroException extends Exception { DivideByZeroException(String msg) { super(msg); } DivideByZeroException() {} }

38 38 Example Program Using Exceptions class Rational { private int numerator, denominator public Rational(int num, int denom) throws DivideByZeroException { if(denom == 0) { throw new DivideByZeroException("Constructor error: “ + num + "/“ + denom); } numerator = num; denominator = denom; } public Rational times(Rational f) { Rational result = null; try { result = new Fraction(numerator*f.numerator, denominator*f.denominator); } catch(DivideByZeroException e) { } return result; } … } class Rational { private int numerator, denominator public Rational(int num, int denom) throws DivideByZeroException { if(denom == 0) { throw new DivideByZeroException("Constructor error: “ + num + "/“ + denom); } numerator = num; denominator = denom; } public Rational times(Rational f) { Rational result = null; try { result = new Fraction(numerator*f.numerator, denominator*f.denominator); } catch(DivideByZeroException e) { } return result; } … }

39 39 Example Program Using Exceptions class Rational { private int numerator, denominator; public Rational divide(Fraction f) throws DivideByZeroException { Rational result = null; if(f.numerator == 0) { throw new DivideByZeroException("Can't divide " + this + " by " + f); } try { result = new Rational(numerator*f.denominator, denominator*f.numerator); } catch(DivideByZeroException e) { } return result; } public String toString() { return numerator + "/" + denominator; } … } class Rational { private int numerator, denominator; public Rational divide(Fraction f) throws DivideByZeroException { Rational result = null; if(f.numerator == 0) { throw new DivideByZeroException("Can't divide " + this + " by " + f); } try { result = new Rational(numerator*f.denominator, denominator*f.numerator); } catch(DivideByZeroException e) { } return result; } public String toString() { return numerator + "/" + denominator; } … }

40 40 Example Program Using Exceptions public class TestProgram { public static void main(String[] args) { // Demonstration of exception handling in client code Rational f1, f2, f3, f4; try { f1 = new Rational(3, 5); f2 = new Rational(1, 2); f3 = f1.divide(f2); f4 = f1.times(f2); System.out.println(f1+" divided by "+f2+" equals "+f3); System.out.println(f1+" times " +f2+" equals "+f4); } catch(DivideByZeroException dbz) { System.out.println("DivideByZeroException:"); System.out.println(dbz.getMessage()); } catch(Exception me) { System.out.println("Exception:"); System.out.println(me.getMessage()); } public class TestProgram { public static void main(String[] args) { // Demonstration of exception handling in client code Rational f1, f2, f3, f4; try { f1 = new Rational(3, 5); f2 = new Rational(1, 2); f3 = f1.divide(f2); f4 = f1.times(f2); System.out.println(f1+" divided by "+f2+" equals "+f3); System.out.println(f1+" times " +f2+" equals "+f4); } catch(DivideByZeroException dbz) { System.out.println("DivideByZeroException:"); System.out.println(dbz.getMessage()); } catch(Exception me) { System.out.println("Exception:"); System.out.println(me.getMessage()); } Try the following: 1.f1 = new Fraction(3,0); 2.f2 = new Fraction(0, 2); Try the following: 1.f1 = new Fraction(3,0); 2.f2 = new Fraction(0, 2);


Download ppt "EXCEPTIONS There's an exception to every rule.. 2 Introduction: Methods  The signature of a method includes  access control modifier  return type "

Similar presentations


Ads by Google