Exception

Exception : The term exception means “exceptional condition” and is an occurrence that alters the normal program flow.
As per java specification : An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions.

Exception Hierarchy





The Causes of Exceptions
An exception is thrown for one of three reasons:

1) An abnormal execution condition was synchronously detected by the Java virtual machine. Such conditions arise because:
  evaluation of an expression violates the normal semantics of the language,  such as an integer divide by zero
  an error occurs in loading or linking part of the program
  some limitation on a resource is exceeded, such as using too much memory

2) A throw statement was executed.
3) An asynchronous exception occurred either because:
the method stop of class Thread was invoked
an internal error has occurred in the virtual machine

Type of exceptions  
Checked Exceptions : 

Java compiler will check the exception at compile time and these exceptions are subject to the Catch or Specify Requirement. 

A compiler for the Java programming language checks, at compile time, that a program contains handlers for checked exceptions, by analyzing which checked exceptions can result from execution of a method or constructor. For each checked exception which is a possible result, the throws clause for the method  or constructor must mention the class of that exception or one of the superclasses of the class of that exception.

All exceptions are checked exceptions, except for those indicated by Error, RuntimeException, and their subclasses. 

UnChecked Exceptions : Those unchecked exception classes which are the error classes (Error and its subclasses) and runtime exception classes (RuntimeException and its subclasses) are exempted from compile-time checking because they can occur at many points in the program and recovery from them is difficult or impossible.

For example, certain code might implement a circular data structure that, by construction, can never involve null references; the programmer can then be certain that a NullPointerException cannot occur, but it would be difficult for a compiler to prove it. 

another example : OutOfMemoryError - Thrown when the Java Virtual Machine cannot allocate an object because it is out of memory, and no more memory could be made available by the garbage collector.

Handling Exceptions

Using try and catch

The try is used to define a block of code in which exceptions may occur. This block of code is called a guarded region (which really means “risky code goes here”). One or more catch clauses
match a specific exception (or class of exceptions—more on that later) to a block of code that handles it.
1. try {
2. // This is the first line of the "guarded region"
3. // that is governed by the try keyword.
4. // Put code here that might cause some kind of exception.
5. // We may have many code lines here or just one.
6. }
7. catch(MyFirstException) {
8. // Put code here that handles this Exception.
9. // This is the next line of the exception handler.
10. // This is the last line of the exception handler.
11. }
12. catch(MySecondException) {
13. // Put code here that handles this exception
14. }
15.
16. // Some other unguarded (normal, non-risky) code begins here

lines 2 through 5 constitute the guarded region that
is governed by the try clause. Line seven is an exception handler for an exception
of type MyFirstException.

Execution starts at line 2. If the program executes all the way to line 5 with no
exceptions being thrown, execution will transfer to line 15 and continue downward.
However, if at any time in lines 2 through 5 (the try block) an exception is thrown
of type MyFirstException, execution will immediately transfer to line 8. Lines 8
through 10 will then be executed so that the entire catch block runs, and then
execution will transfer to line 15 and continue.
Note that if an exception occurred on, say, line 3 of the try block, the rest of the
lines in the try block (3 through 5) would never be executed. Once control jumps
to the catch block, it never returns to complete the balance of the try block.
This is exactly what you want, though.

Using finally

A finally block encloses code that is always executed at some point after the
try block, whether an exception was thrown or not. Even if there is a return statement
in the try block, the finally block executes right after the return statement! This
is the right place to close your files, release your network sockets, and perform any
other cleanup your code requires. If the try block executes with no exceptions, the
finally block is executed immediately after the try block completes. If there
was an exception thrown, the finally block executes immediately after the proper
catch block completes.

try {
// do stuff
} catch (SomeException ex) {
// do exception handling
} finally {
// clean up
}






No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...