Presentation is loading. Please wait.

Presentation is loading. Please wait.

BIO Java 1 Exception Handling Aborting program not always a good idea – E-mail: can’t lose messages – E-commerce: must ensure correct handling of private.

Similar presentations


Presentation on theme: "BIO Java 1 Exception Handling Aborting program not always a good idea – E-mail: can’t lose messages – E-commerce: must ensure correct handling of private."— Presentation transcript:

1 BIO Java 1 Exception Handling Aborting program not always a good idea – E-mail: can’t lose messages – E-commerce: must ensure correct handling of private info in case of crash – Antilock braking, air-traffic control: must recover and keep working Java includes mechanisms for recovering from exceptions

2 BIO Java 2 Exceptions in Java Exceptions are (special) objects in Java They are created from classes The classes are derived from a special class, Throwable

3 BIO Java 3 Java Exception Hierarchy Throwable The base class for all exceptions, it is required for a class to be the rvalue to a throw statement. Error Any exception so severe it should be allowed to pass uncaught to the Java runtime. Exception Anything which should be handled by the invoker is of this type, and all but five exceptions are.

4 BIO Java 4 Java Exception Terminology When an anomaly is detected during program execution, the JVM throws an exception – There are built-in exceptions – Users can also define their own To avoid crashing, a program can catch a thrown exception An exception generated by a piece of code can only be caught if the program is alerted. This process is called trying the piece of code

5 BIO Java 5 Exception Handling When an error occurs in a Java method, the method creates an object of type Exception and gives it to the run-time system. The Exception object consists of the information pertaining to the exception, like its type and the state of the program when the exception occurred.

6 BIO Java 6 Exception Handling The run-time system looks for the appropriate method that would handle the error occurred. The process of creating an Exception object and handling it to the run-time system is called throwing an exception.

7 BIO Java 7 Exception Handling The method that handles the exception thrown is called an exception handler. The exception handler is used to catch the exception thrown.

8 BIO Java 8 Exception Handling The run-time system searches for the exception handler, starting from the method where the error occurred, the search goes on…till an appropriate handler is found. If not found, then the run-time system terminates the application.

9 BIO Java 9 9 Keywords for Java Exceptions throws Describes the exceptions which can be raised by a method. throw Raises an exception to the first available handler in the call stack, unwinding the stack along the way. try Marks the start of a block associated with a set of exception handlers. catch If the block enclosed by the try generates an exception of this type, control moves here; watch out for implicit subsumption. finally Always called when the try block concludes, and after any necessary catch handler is complete.

10 BIO Java 10 try-catch block Program statements that we want to monitor for exceptions are contained within a try block. If an exception occurs with-in the try block, it is thrown. Our code can catch this exception and handle it in some rational manner.

11 BIO Java 11 An Example …. try { int a=0; int b=5; System.out.println("Div=" + (b/a)); } catch (ArithmeticException e) { //code to handle }

12 BIO Java 12 throw, throws, finally System generated exceptions are automatically thrown by the JRE. To manually throw an exception, use the keyword throw. An exception that is thrown out of a method must be specified as such by a throws clause. Any code that absolutely must be executed before a method returns is put in a finally block

13 BIO Java 13 throw, throws, finally System generated exceptions are automatically thrown by the JRE. To manually throw an exception, use the keyword throw. An exception that is thrown out of a method must be specified as such by a throws clause. Any code that absolutely must be executed before a method returns is put in a finally block

14 BIO Java 14 Checked & Unchecked Exceptions Unchecked exceptions – any exceptions that is derived from class Error and class RunTimeException. All other exceptions are called checked exceptions. The compiler checks that exception handlers are there for all checked exceptions.

15 BIO Java 15 Checked Exceptions Java method can throw an exception if it encounters a situation it cant handle. A method not only declares its signature but it also tells the compiler what can go wrong. (the exceptions which it may throw). throws clause at the header part of the method states the exceptions which it throws. Eg. void m(int a) throws IOException

16 BIO Java 16 Throwing an Exception The keyword throw is used to throw an exception from a method. Find an appropriate exception class. Make an object of that class. Throw it.

17 BIO Java 17 finally finally clause – creates a block of code that will be executed after a try/catch block is completed and before the code following the try/catch block.


Download ppt "BIO Java 1 Exception Handling Aborting program not always a good idea – E-mail: can’t lose messages – E-commerce: must ensure correct handling of private."

Similar presentations


Ads by Google