Java Handling Abw
Java Handling Abw
1. Introduction
2. Runtime Stack Mechanism
3. Default exception handling in java
4. Exception hierarchy
5. Customized exception handling using Try and Catch block
6. Control flow in Try Catch
7. Methods to print exception information
8. Try with multiple Catch blocks
9. Finally Block
10. Difference between Final, Finally and Finalize
11. Various possible combinations of Try-Catch-Finally
12. Control flow in Try-Catch-Finally
13. Control flow in nested Try-Catch-Finally
14. Throw keyword
15. Throws keyword
16. Exception handling keyword summary
17. Various possible errors in exception handling in compile time
18. Customized or user defined exceptions
19. Top 10 Exceptions
20. 1.7 version enhancements
Try with resources
Multi-catch block
1. Introduction
An unwanted, unexpected event that disturbs the normal execution of the
program is called exception.
Exception Handling
Graceful termination of the program
If something goes wrong we should not miss anything, we should not lose
any resources, we should not block any resources and graceful termination
is required then we should go for the Exception handling.
To define alternative ways to execute the remaining program normally is
nothing but exception handling.
2. Runtime Stack Mechanism
Every java program is at least have one thread that is the Main thread.
For every thread JVM will create one runtime stack
For every method m1 present in that thread JVM will stored one entry in
the Stack this entry is called Stack Frame or Activation record.
Once the m1 method is completed the entry in the stack will be removed.
After all the methods present inside the thread completed that thread will
be terminated.
Before terminating the thread JVM will destroy the empty Stack which
created for that thread.
3. Default Exception Handling
If inside a method an exception raised the method in which it is raised is
responsible to create the exception object with the help of JVM.
In this object information will be added like below,
e.g.
public class DemoPgm {
public static void main(String[] args) {
doStuff();
}
public static void doStuff(){
doMoreStuff();
}
public static void doMoreStuff(){
System.out.println(10/0);
}
}
2. e.toString() or just e
If you just want name of exception and the description we can use this
method
For this method System.Out.println is required to print the message
3. e.getMessage()
If you only want the description of the exception the we can use this
method
For this method System.Out.println is required to print the message
These methods we can apply any exception object and any error object.
These methods are available in Throwable class
try with multiple catch blocks
If in our try block multiple exceptions raise chances are there, like
FileNotFoundException, ArithmeticException etc only catch block will not
be sufficient to handle the exceptions.
So in this case for multiple exceptions multiple catch block should be
provided so that for particular exception corresponding catch block will
be executing.
At the end one default (Exception e) exception catch block also should be
there in case other catch blocks are failing means it will execute.
Loophole in multiple catch block
In try with multiple catch blocks the order of catch block is very
important
The order should be from child to parent not from parent to child
try{
int i=10;
System.out.println(i/0);;
}catch (Exception e)
{
System.out.println(e);
}catch (ArithmeticException e)
{
System.out.println(e);
}
here in this case compiler give the error as AE exception has been already
caught