Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 ָ נן oop uException-Handling Mechanism – similar to C++ l throw e signals the occurrence of an exception lThe statement try/catch allows the calling.

Similar presentations


Presentation on theme: "1 ָ נן oop uException-Handling Mechanism – similar to C++ l throw e signals the occurrence of an exception lThe statement try/catch allows the calling."— Presentation transcript:

1 1 ָ נן oop uException-Handling Mechanism – similar to C++ l throw e signals the occurrence of an exception lThe statement try/catch allows the calling method to “catch” the “thrown” exception object Exceptions in Java public static void main(String[] args) try{ // Open a file: FileReader f = new FileReader(args[0]); System.out.println(args[0] + " opened"); f.close(); } catch (IOException x){ System.out.println(x); }

2 2 ָ נן oop The finally Clause uFor code that must ALWAYS run lRegardless of which catch block executed lEven if no catch block executed lEven if a return or break occurs first lException: System.exit( ) lExecutes before transferring control to caller uPlaced after handlers (if they exist) ltry-block must either have a handler or a finally-block ucan be used to clean up the programming environment void n() { try { … open window … p() … } catch (SomeException se) { … } finally { … close window … } } void p() { … throw se … }

3 3 ָ נן oop Throwing Exceptions uThrown objects must derive (ultimately) from Throwable lProvides methods like printStackTrace() uUsually derive from java.lang.Exception uTry can catch any exception using the following code: uBe careful: Just like C++, Java executes the first catch statement capable of handling the exception try { … } catch (Exception e) { … handle any type of exception … } try { … } catch (Exception e) { … } catch (ArrayIndexOutOfBounds ae) { … } the second handler is never executed

4 4 ָ נן oop Exception Hierarchy InterruptedException ArithmeticException Throwable Object ParseException IOException Exception RunTimeException IndexOutOfBoundsException ArithmeticException NullPointerException

5 5 ָ נן oop Checked vs. Unchecked Exceptions uChecked exceptions lErrors a program should handle lAre subclasses of Exception lCompiler requires “catch or declare” Catch and handle the exception in method, OR Declare method can throw exception void A() throws ExceptionType { … } uUnchecked exceptions lRepresent defects in the program lAre subclasses of RuntimeException lMethods are not required to declare or handle

6 6 ָ נן oop Exceptions and Inheritance uMethods overridden in subclasses must maintain the parent method’s contract lsubstitutability lcannot add exceptions to specification lcan omit, however lcan throw subclasses of parent’s exceptions class Parent{ public void f() throws Exception {} } class Child extends Parent{ public void f()// OK! {} }

7 7 ָ נן oop Quiz public void m() throws Exception{ try { System.out.println("m's throw"); throw new Exception(); } catch(Exception E){ System.out.println("m's handler"); throw E; } finally{ System.out.println("m's finally"); } public void n() { try { System.out.println("n calling m"); m(); } catch(Exception E){ System.out.println( “ n's handler"); } finally{ System.out.println( “ n's finally"); } What is the output of a call to n() ?

8 8 ָ נן oop Exceptions in Eiffel uEiffel is based on a “design by contract” concept lRoutine preconditions - must be true upon calling a method. lRoutine postconditions - must be true at the end of a method. lClass invariants - must be satisfied at all stable times. uA routine that is unable to meet its contract fails. lThe failure of an operation is an exception for the routine that needed the operation. uThe rescue keyword defines an exception handler lWithin a rescue block one can retry the routine. retry starts again the execution of the routine, without repeating the initialization of local entities uIf a precondition is not met, the rescue block must be defined in the calling routine.

9 9 ָ נן oop An Example read_next_character(f: FILE) is -- Make next character available in last_character; require readable: file.readable local attempts: INTEGER do last_character:= low_level_read_function(f) rescue attempts:= attempts + 1 if attempts < Max_attempts then retry end if false, read_next_character fails and the exception must be handled in the calling routine the file exists and is accessible for reading

10 10 ָ נן oop Programmer Exceptions uA class that derives from class Exceptions can explicitly trigger exceptions l raise (name: STRING) - raises a developer exception of name name l developer_exception_name : STRING – retrieves the name of last developer-raised exception to detect and process specific exceptions.


Download ppt "1 ָ נן oop uException-Handling Mechanism – similar to C++ l throw e signals the occurrence of an exception lThe statement try/catch allows the calling."

Similar presentations


Ads by Google