Exception Handling
We don’t like exceptions but we always have to deal with them, so Exception handling in Java is very robust and easy to understand and use.
You need to handle the exceptions for make it understandable to the users.
Exception may occur due to Network Failure, Database Server down, wrong data entered by user, Hardware Failure etc.
What is Exception ?
Runtime Errors are known as Exception.
An Abnormal condition is called exception.
Exception Api exist on java.lang package.
Exceptions are recoverable.
Mostly Exceptions are generated after Deployment when application used by client.
Like ArithmeticException, NullPointerException etc.
What is Error ?
Errors are also generated at Runtime.
Errors are not recoverable.
Like StackOverFlowError, OutOfMemoryError etc.
Exception Hierarchy
Type of Exception
There are two type of exception:
- Checked or Caught or Compile time exception
- Unchecked or Uncaught or Runtime exception
1. Checked or Caught or Compile time exception
Those exception which is force you to handle at compile time that is called Checked or Caught or Compile time exception.
Those exception who extends Exception class except of RuntimeException class that is called Checked or Caught or Compile time exception.
Like : FileNotFoundException, IOException, SQLException, ClassNotFoundException etc.
2. Unchecked or Uncaught or Runtime exception
Those exception which is never force you to handle at compile time that is called Unchecked or Uncaught or Runtime exception.
Those exception who extends RuntimeException class that is called Unchecked or Uncaught or Runtime exception.
Like : ArithmeticException, NullPointerException, NumberFormatException, ArrayIndexOutOfBoundsException etc.
How many way to handle an Exception ?
There are two way:
- Default Handler or JVM Handling
- User Handling
1. Default Handler or JVM Handling
In Java Exceptions are handled by default using DefaultHandler or JVM.
In case any kind of problems are generated, DefaultHandler handle all things but there are two problems with DefaultHandler or JVM.
- System Specific Message are generated
- No rest of code execution
How DefaultHandler or JVM works internally ?
Firstly create instance of Exception class
Then call the printStackTrace() method of Exception class.
Then Exit controller from system using System.exit(0) method.
Thats why no rest of code exeecuted.
2. User Handling
Basically User Handler works on top in DefaultHandler or JVM.
In case User Handler it solve the DefaultHandler problems.
- User Specific Message are generated
- Rest of code execution
2. User Handlers exception managed by following way:
- try catch block
- throws
- throw
- finally block
- Chaning Exception
- Custom Exception
- Log File
In JAVA 1.7 there are new enhancement:
- try with resources
- single catch with multiple exception declaration
1. try catch block
using try catch block you can handle an exception.
try is a keyword or block where you can write that code you think exception can be occured.
catch is a keyword or block from where you can handle the exception.
try catch block is used when you Deployment an application.
try catch block never propogated means when once handle you dont need to handle from different place.
try catch block is used with CheckedException or UncheckedException both.
There are some following rules you need to follow:
- try block can never use alone.
- try block can be used with catch.
- try block can used with finally.
- try block can used with catch and finally respectively.
- You can used single try with multiple catch block in an order(sub class then super class).
- You can never use multiple try with single catch block.
- You can never write a statement between try and catch block.
- You can use return statement between try block.
- You can never used single try with multiple finally block.
- You can used nested try catch block.
- You can used try catch block with in a method body, constructor, block etc.
try{ } //you get compile time error
try{ }catch(Exception e){ }
try{ }finally{ }
try{ }catch(Exception e){ }finally{ }
try{ }catch(ArithmeticException e){ }catch(NullPointerException e){ }catch(Exception e){ }
try{ }try{ }catch(Exception e){ }//compile time error
try{ } System.out.println("Hello"); catch(Exception e){ }//compile time error
try{ return; }catch(Exception e){ }
try{ }finally{ }finally{ }//compile time error
try{ try{ }catch(Exception e){ } }catch(Exception e){ }
class Test{ //constructor Test(){ try{ }catch(Exception e){ } } //method body void foo(){ try{ }catch(Exception e){ } } }
2. throws keyword
throws keyword is used to declare an exception.
throws keyword is used to declare more than one exception separated by ,.
void show()throws IOException,SQLException{ }
throws keyword is used with the method and constructor signature.
throws keyword is used with the CheckedException only.
you can also used throws keyword and try catch block in a same block.
void show()throws IOException,SQLException{ try{ }catch(ArithmeticException e){ } }
Difference between try catch vs throws keywords
try catch block | vs | throws keyword |
---|---|---|
it can be used within the body. | it can be used with the method or constructor signature. | |
it can be used with CheckedException or UncheckedException. | it can be used with CheckedException only. | |
try catch block is used to handle an exception. | throws keyword is used to declare an exception. | |
it can handle only one exception at a time. | it can be declare multiple exception. | |
it can not be propogated. | it can be propogated. | |
it can be used when you Deployment an application. | it can be used when you Development an API. |
3. throw keyword
throw keyword is used to throw an exception explicit.
throw keyword is used with in the body.
DefaultHandler also used throw keyword to throw an exception.
Using throw keyword we can rethrow an exception.
throw keyword is used only with exception object.
4. finally block
finally block is used for executing a task either exceptions are occur or not.
finally block is used for closing the connection, cleanup the resources etc.
finally block is a block that must be executed.
finally block always used with try block.
5. Chaining Exception
When an exception occur another exception that is called Chaining exception.
Chaining exception is used to show the cause of exception.
Using initCause() method you can set an underlying cause with invoking exception.
Using getCause() method you can get the actual cause associated with current exception.
You can acheive Chaining exception throug throw keyword.
6. Custom Exception
In java you can also create own exception.
You can create both type of exception either Checked or Unchecked.
In case of CheckedException you need to extends any CheckedException class.
In case of UncheckedException you need to extends any UncheckedException class.
For Example:
class DeepException extends Exception{}
class DeepException extends RuntimeException{}
7. Log File
Using Log File you can maintain code easly.
This log file used for developer to find out the problems.
This is a part of file handling.
This can be acheive by printStackTrace(PrintStream) or printStackTrace(PrintWriter) method.
3 comments:
Thanks sir
Thank you sir , good blog
Very good post sir. Please explain semaphore concept in java
Post a Comment