Presentation is loading. Please wait.

Presentation is loading. Please wait.

12.1 Exceptions. 12.1.1 The limitations of traditional methods of exception handling Error conditions are a certainty in programming Programmers make.

Similar presentations


Presentation on theme: "12.1 Exceptions. 12.1.1 The limitations of traditional methods of exception handling Error conditions are a certainty in programming Programmers make."— Presentation transcript:

1 12.1 Exceptions

2 12.1.1 The limitations of traditional methods of exception handling Error conditions are a certainty in programming Programmers make errors & users make errors Best to catch error conditions at compile time, if possible Minimal steps in the event of a runtime error Notify the user Save all of the work Allow the user to exit OOP uses objects to represent error conditions

3 12.1.2 Error handling in the Java platform Objects used to represent errors are Exceptions Represent an unexpected result or condition Exception handling is assigned to specific code blocks

4 12.2.1 Types of errors JVM errors –Possible problems with the language or virtual machine Code or Data errors –Arithmetic, casting, etc Standard errors –“Expected Exceptions,” eg, file not found User errors –Invalid usage

5 12.3.1 Throwing and handling exception objects Generating an object to represent an error condition is know as “Throwing an Exception” When Exceptions are thrown, the program searches for the code that deals with it If your code could generate an exception, you must “handle or declare” it

6 12.4.1 Class throwable Throwable is the superclass of all objects that can be thrown Of all the Throwable subclasses, only Exception is checked at compile time Programmers are not expected to handle Errors

7 12.5.1 Advertising exceptions Any method that could generate an error should be able to throw an exception (if y == 0.0) { throw new zeroException(); } The method signature must also declare the possibility of the exception public void divideTwoNumbers(double x, double y) throws zeroException { … }

8 12.5.2 Handle and declare To use a method that may throw an exception, you can write code to handle it try { double x = 15.0; double y = 0.0; double z = divideTwoNumbers(x, y); System.out.println(“Division ok. “); } catch(zeroException e) { System.out.println(“Couldn’t divide those numbers. “); } finally { System.out.println(“Finished the test.”); }

9 12.5.3 Using the try-catch block Code that can result in an exception should be in a ‘try’ block When an exception is encountered, execution leaves the try block immediately, and resumes in the catch block Multiple catch blocks are permitted Allows for several types of exceptions to be caught and handled

10 12.6.1 Creating an exception class of objects Creating your own exceptions simply involves subclassing Exception In your code, use a conditional test that throws your new exception when the error occurs Calling methods can either catch your new exception, or simply catch the Exception superclass

11 12.7.1 The order of exception blocks

12 12.7.2 Execution sequence The finally block is almost always executed, even when there is a return in the try block The finally block is not executed when: –An exception is thrown in the finally block –The Thread dies –System.exit() is used in the try block –The computer is powered off

13 12.7.3 Execution when an exception is thrown Execution stops at the line where the exception occurred Continues in the first catch block that declares the type of exception thrown The finally block is called, even if the catch block has a return

14 12.8.1 Rules for overriding methods Overriding methods can throw the same exceptions as the parent method Overriding methods can throw subclasses of the exceptions the parent method throws Overriding methods can throw fewer exceptions than the parent method


Download ppt "12.1 Exceptions. 12.1.1 The limitations of traditional methods of exception handling Error conditions are a certainty in programming Programmers make."

Similar presentations


Ads by Google