Tutorial/SCJP-Tutorial-for-Exception-Handling
Tutorial : SCJP Tutorial for Exception Handling

Defination :- An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions.
There are 3 main advantages for exceptions:
1. Separates error handling code from "regular" code
2. Propagating errors up the call stack (without tedious programming)
3. Grouping error types and error differentiation
Syntax :-
try {
int y =5;
for(int i =5; i<=0; i++)
{ int z = x/i; }
}catch(Exception e1)
{ system.out.println( "Exception Occured" +e.printstacktrace());
}
finally {
System.out.println("Fional code is always executed");
}
Important tricks to be Noted :-
-
" An exception causes a jump to the end of try block. If the exception occurred in a method called from a try block, the called method is abandoned.
-
" If there's a catch block for the occurred exception or a parent class of the exception, the exception is now considered handled.
-
" At least one 'catch' block or one 'finally' block must accompany a 'try' statement. If all 3 blocks are present, the order is important. (try/catch/finally)
-
" finally and catch can come only with try, they cannot appear on their own.
-
" Regardless of whether or not an exception occurred or whether or not it was handled, if there is a finally block, it'll be executed always. (Even if there is a return statement in try block).
-
" System.exit() and error conditions are the only exceptions where finally block is not executed.
-
" If there was no exception or the exception was handled, execution continues at the statement after the try/catch/finally blocks.
-
" If the exception is not handled, the process repeats looking for next enclosing try block up the call hierarchy. If this search reaches the top level of the hierarchy (the point at which the thread was created), then the thread is killed and message stack trace is dumped to System.err.
-
" Use throw new xxxException() to throw an exception. If the thrown object is null, a NullPointerException will be thrown at the handler.
-
" If an exception handler re-throws an exception (throw in a catch block), same rules apply. Either you need to have a try/catch within the catch or specify the entire method as throwing the exception that's being re-thrown in the catch block. Catch blocks at the same level will not handle the exceptions thrown in a catch block - it needs its own handlers.
-
" The method fillInStackTrace() in Throwable class throws a Throwable object. It will be useful when re-throwing an exception or error.
-
" The Java language requires that methods either catch or specify all checked exceptions that can be thrown within the scope of that method.
-
" All objects of type java.lang.Exception are checked exceptions. (Except the classes under java.lang.RuntimeException) If any method that contains lines of code that might throw checked exceptions, compiler checks whether you've handled the exceptions or you've declared the methods as throwing the exceptions. Hence the name checked exceptions.
-
" If there's no code in try block that may throw exceptions specified in the catch blocks, compiler will produce an error. (This is not the case for super-class Exception)
-
" Java.lang.RuntimeException and java.lang.Error need not be handled or declared.
Classification of Exception:
Exception-->ClassNotFoundException, ClassNotSupportedException, IllegalAccessException, InstantiationException, IterruptedException, NoSuchMethodException, RuntimeException, AWTException, IOException
RuntimeException-->EmptyStackException, NoSuchElementException, ArithmeticException, ArrayStoreException, ClassCastException, IllegalArgumentException, IllegalMonitorStateException,
IndexOutOfBoundsException, NegativeArraySizeException, NullPointerException, SecurityException.
IllegalArgumentException-->IllegalThreadStateException, NumberFormatException
IndexOutOfBoundsException-->ArrayIndexOutOfBoundsException, StringIndexOutOfBoundsException
IOException-->EOFException, FileNotFoundException, InterruptedIOException, UTFDataFormatException, MalformedURLException, ProtocolException, SockException, UnknownHostException, UnknownServiceException.
- guru's blog
- Login to post comments
