Documente Academic
Documente Profesional
Documente Cultură
Feb 25, 26
Namal College An Associate College of University of Bradford Talagang Road, Mianwali, Pakistan
Introduction
A program can be written assuming that nothing unusual or exceptional will happen:
User will provide the correct input Stack will never overflow File will be there to be opened Network connection will be established whenever needed and so on.
In reality, these assumptions will never always hold A good program is the one which can cater for exceptional situations so that it is robust and fault-tolerant
2
Introduction
Heres what you might like to do:
open a text file read a line from the file
All this error checking and handling of exceptional situations really gets in the way of developing a robust and usable software
3
What is an Exception?
An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions. Either your code or the Java Virtual Machine (JVM) signals when something unusual happens. The signaling process is called throwing an exception. Somewhere in your program, you can place code to handle the exception. This is called catching the exception.
SD-2, Spring - 2014
} catch(ExceptionType e) {
// code to handle the exception
} finally {
// typically, code to avoid resource leakage
try-catch rules
The first step in constructing an exception handler is to enclose the code that might throw an exception within a try block. A catch block handles the exception. This code executes when the try block detects the exception.
No code can be between the end of the try block and the beginning of the first catch block
If an exception occurs in a try block, the try block terminates immediately and program control transfers to the first catch block with a parameter type that matches the thrown exceptions type. After the execution of catch block, the program control does not return to the throw point in try block
6
try-catch Rules
If there are multiple matching catch blocks, then upon occurrence of exception, only the first one matching is executed. The rest are simply skipped.
try { // code that can throw exception } catch(SpecificExceptionType e){ // code that can throw exception } catch(MoreGenericExceptionType e){ // code that can throw exception } catch(MostGenericExceptionType e){ // code that can throw exception SD-2, Spring - 2014 }
The specific the catch block exception, the higher should its position be
The finally block will execute whether or not an exception is thrown in the corresponding try block
If an exception cannot be caught by one of the catch handlers, control proceeds to the finally block. Then the exception is passed to the next outer try block. If a catch block throws an exception, the finally block still executes. Then the exception is passed to the next outer try SD-2, Spring - 2014 8 block.
How it works?
After a method throws an exception, the Java runtime system attempts to find a method that contains a block of code (catch block) that can handle the exception. The search begins with the method in which the error occurred and proceeds through the method call stack in the reverse order in which the methods were called. When an appropriate handler is found, the runtime system passes the exception to the handler.
How it works?
If no catch block is found which can match the exception type, the runtime system reports an error with the stack trace and program may terminate.
10
Uncaught Exception
An uncaught exception is the one for which there is no matching catch block
If a program has only one thread, an uncaught exception will cause the program to terminate.
If a program has multiple threads, an uncaught exception will terminate only the thread where the exception occurred.
In such programs, however, certain threads may rely on others, and if one thread terminates due to an uncaught exception, there may be adverse effects to the rest of the program
SD-2, Spring - 2014 11
Exception Hierarchy
12
Class Throwable
The Throwable class is the superclass of all errors and exceptions in the Java language. Only objects that are instances of this class (or one of its subclasses) are thrown by the JVM or can be thrown by the Java throw statement. Only this class or one of its subclasses can be the argument type in a catch clause.
13
Not all exceptions are subject to the Catch or Specify Requirement. Code that does not fulfil the Catch or Specify Requirement will not compile.
14
Categories of Exceptions
Two main categories
Checked exceptions Unchecked exception
Checked Exceptions
exceptional conditions that a well-written application must anticipate and recover from. For example, file system access, network access, etc. Checked exceptions are subject to the Catch or Specify Requirement
Categories of Exceptions
Unchecked Exceptions
Errors
exceptional conditions that are external to the application and that the application usually cannot anticipate or recover from. For example, hardware fault, VM error etc.
Runtime Exceptions
exceptional conditions that are internal to the application and that the application usually cannot anticipate or recover from. For example, logical or semantic errors, incorrect use of APIs
Errors and Runtime exceptions are not subject to the Catch or Specify Requirement
Categories of Exceptions
Throwable
Error
Need not be caught
Exception
Must be caught
RuntimeException
17
Throwing Exceptions
Before you can catch an exception, some code somewhere must throw one. Any code can throw an exception, e.g.
Java runtime environment code from a package written by someone else such as the packages that come with the Java platform, or even your own code
Regardless of what throws the exception, it's always thrown with the throw statement.
SD-2, Spring - 2014 19
throw statement
All methods use the throw statement to throw an exception.
throw someThrowableObject;
The throw statement requires a single argument: a throwable object which is an instance of any subclass of the Throwable class.
20