java tutorial

java tutorial

Java Exception Handling

Exception Handling

To handle run-time errors JAVA has exception handling mechanism to handle the flow of the programs.

Pre- Defined Exceptions 

So as to manage synchronous and asynchronous special cases, JAVA consists of java.lang.Error class. Synchronous are of two types:

  • Checked exceptions which are covered during Compilation of the program
  • Unchecked exceptions are found during Run time of the program like NoClassFoundException etc. 


Try block in JAVA code encloses the thrown exceptions while Catch block is to catch them. While Finally contains the codes to execute commands like closing resultsets or connections.

//code to be written
}catch(Exception_name e){
//error message
}  finally(){
//code part

Throws and Throw

Throws keyword in JAVA declares an exception while Throw keyword explicitly throws the same

User- Defined Exceptions Examples

 Sometimes, the user creates its own custom exception according to the project. Following is the example of creating a user-defined exception:

class DummyException{
   public static void main(String args[]){
       throw new CustomException(2);

 catch(CustomException e){
    System.out.println(e) ;
class CustomException extends Exception{
   int b;
   CustomException(int c) {
   public String toString(){
     return ("Print Exception Number here =  "+b) ;

Leave a Reply

Your email address will not be published. Required fields are marked *