EXCEPTION HANDLING IN JAVA:

Exception means runtime error.
This is not occears at compile time.
In general, an exception represents something that happens not as a result of
a programming error, but rather because some resource is not available or some
other condition required for correct execution is not present.
For example, if you call a method that opens a file and write something but the file does not exist, execution of that method will stop, and code that you wrote to deal with this situation will be run.In this situation exception is thrown by JVM When you try to save something in the database but the database is not open then exception is thrownby the JVM at runtime.
Exception handling works by transferring the execution of a program to an appropriate exception handler when an exception occurs.

USING try,catch and finally TO HANDLE EXCEPTION:

We need a way to tell the JVM what code to execute when a certain exception happens.
That is nothing but exception handling.
try {
//open a file
//in the below line exception occears
//write something(file is read only)
}
catch(MyFirstException) {
//Print what exception occears
}
catch(MySecondException) {
// print what exception occears
}
catch(Exception){//some code here}
finally{//close file}
The try is used to define a block of code in which exceptions may occur.
Catch block is used to catch the exception which occeared in the try block.
In the above code there are three catch blocks.
Actually in the program which exception will occear we do not know.So we have to specify catch block for all the expected exceptions.So which exception occears then the relative catch block handles it.
The last catch block contains only Exception,this the super class of the exceptions. When the exception occeared in the code does not match the above two catch blocks then the last statement handles the exception.
A finally block encloses code that is always executed at some point after the
try block, whether an exception was thrown or not.finally is used to clean up. This is the right place to close your files, release your network sockets, and
perform any other cleanup your code requires.Because execution transfers out of the try block as soon as an exception is thrown, we can't put our cleanup code at the bottom of the try block and expect it to be executed if an exception occurs.
If the try block executes with no exceptions, the finally block is executed immediately after the try block completes.

-The only exception to the finally-will-always-be-called rule is that a finally
will not be invoked if the JVM shuts down. That could happen if code from the try or catch blocks calls System.exit().
-Just because finally is invoked does not mean it will complete. Code in the
finally block could itself raise an exception or issue a System.exit().
-if you have no resources to clean up after your try block completes, you probably don't need a finally clause.
-compiler doesn't even require catch clauses, sometimes you'll run across code that
has a try block immediately followed by a finally block.
-exceptions are always some subclass of java.lang.Exception.This class derives from the class Throwable.
-In the catch block hirarchy subclass must be above super class.otherwise it is compilation error.
try{}
catch(Exception e){}//compilation error
catch(IOException e){}
The compiler will stop you from defining catch clauses that can never be reached.
EXCEPTION CLASS HIRARCHY:

-Troowable is in the top of the hirarchy.Two sub classes of Throwable, Error and Exception
--Error-classes that derived form Errorrepresent unsusal situations lthat are not caused by program such as JVM out of memory.Your application won't be able to recover from the error,So you are not required to handle them.
-Exception-All the exceptions can be handled by the program comes under Exception class.
-Exceptions come in two flavors: checked and unchecked.
-checked exceptions, it's commonly said that Java forces developers to handle exceptions.Checked exceptions include all subtypes of Exception, excluding classes that extend RuntimeException.These must either declare the exception using throws, or handle the exception with an appropriate try/catch.
-unchecked Exception-The compiler doesn't enforce the handle or declare rule. You're free to handle them, or to declare them, but the compiler doesn't care.
-Throws:-some method throws an exception that we have to catch?The throws keyword is used as follows to list the exceptions that a method can throw.It is used when we do not want to handle the exception. It throws the exception to the caller of the method.
void myFunction() throws MyException1, MyException2 {
// code for the method here
}
-Throw:-Sometime we intentionaly throw an excetion in the program.In this case tr=hrow keyword is used.
throw new Exception;

A special case :
        If an exception occurs in the try block(without catch), when it goes to finally again an exception occurs in finally bloc, then which exception will be thrown ?
The exception occurred in the finally block will be thrown.
If catch exception occurred in  try, catch and finally then also the finally block exception will be thrown.
When Finally block won't be executed ?
- When System.exit() called
- Thread stopped
- System shutdown

3 comments:

  1. How can you handle the error."Your application won't be able to recover from the error,So you are not required to handle them." then how can you handle the error how the jvm can handle..?

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. great article. Thanks for sharing Exception Handling in Java .its really helpful for me.java is the one of the most programming language build up on api....keep sharing on updated java tutorials?

    ReplyDelete