2
2 Introduction Rarely does a program runs successfully at its very first attempt. It is common to make mistakes while developing as well as typing a program. Such mistakes are categorised as: syntax errors - compilation errors. semantic errors– leads to programs producing unexpected outputs. runtime errors – most often lead to abnormal termination of programs or even cause the system to crash.

3
3 Common Runtime Errors Dividing a number by zero. Accessing an element that is out of bounds of an array. Trying to store incompatible data elements. Using negative value as array size. Trying to convert from string data to a specific data value (e.g., converting string abc to integer value). File errors: opening a file in read mode that does not exist or no read permission Opening a file in write/update mode which has read only permission. Corrupting memory: - common with pointers Any more ….

6
6 Error Handling Any program can find itself in unusual circumstances – Error Conditions. A good program should be able to handle these conditions gracefully. Java provides a mechanism to handle these error condition - exceptions

7
7 Exceptions An exception is a condition that is caused by a runtime error in the program. Provide a mechanism to signal errors directly without using flags. Allow errors to be handled in one central part of the code without cluttering code.

8
8 Exceptions and their Handling When the JVM encounters an error such as divide by zero, it creates an exception object and throws it – as a notification that an error has occurred. If the exception object is not caught and handled properly, the interpreter will display an error and terminate the program. If we want the program to continue with execution of the remaining code, then we should try to catch the exception object thrown by the error condition and then take appropriate corrective actions. This task is known as exception handling.

10
10 Exceptions in Java A method can signal an error condition by throwing an exception – throws The calling method can transfer control to a exception handler by catching an exception - try, catch Clean up can be done by - finally

17
17 finally block Java supports definition of another block called finally that be used to handle any exception that is not caught by any of the previous statements. It may be added immediately after the try block or after the last catch block: … try { // statements } catch( Exception-Type1 e) { // statements to process exception 1 }.. finally { …. } When a finally is defined, it is executed regardless of whether or not an exception is thrown. Therefore, it is also used to perform certain house keeping operations such as closing files and releasing system resources.

18
18 Catching and Propagating Exceptions Exceptions raised in try block can be caught and then they can be thrown again/propagated after performing some operations. This can be done by using the keyword throw as follows: throw exception-object; OR throw new Throwable_Subclass;

26
26 Summary A good programs does not produce unexpected results. It is always a good practice to check for potential problem spots in programs and guard against program failures. Exceptions are mainly used to deal with runtime errors. Exceptions also aid in debugging programs. Exception handling mechanisms can effectively used to locate the type and place of errors.

27
27 Summary Try block, code that could have exceptions / errors Catch block(s), specify code to handle various types of exceptions. First block to have appropriate type of exception is invoked. If no local catch found, exception propagates up the method call stack, all the way to main() Any execution of try, normal completion, or catch then transfers control on to finally block