Saturday, June 16, 2018

Learn Exception Handling in Java

Exception is an event which occurs because of some errors in Java. It is an event which can occur at the time of execution of the code. We can handle exceptions in Java by using some keywords. It’s an easy, flexible, object-oriented and simple software development language. So it supports all the features like classes, objects, inheritance, polymorphism, interfaces etc. 

Whether you are learning Core Java courses or advanced Java courses, understanding exception handling is really an essential topic for learners.

Exception handling is an important feature of Java. Exception handling is used to handle the exceptions which helps in execution of program without any interruption. Errors and exceptions are not the same so let us have a look that what is the difference between error and exception.

Error vs Exception

Error is an event which can not be estimate by any one and there is no mechanism available to handle it. You can say that these are the unchecked exceptions whereas one can estimate that where a exception will occur or arise. To handle these type of exceptions we have some mechanism or we can say that have some keywords. It is also known as checked exceptions. 

Example of errors are OutofMemoryError and IoError and example of exceptions are IOException and RunTimeException. Error and exceptions both are defined under class named as Throwable. Now Let us see how can we handle the exceptions in Java.

Keywords which are used to handle the exceptions are:-

  1. Throw
  2. Throws
  3. Try and catch
  4. Finally

These all of the keywords are used to handle the exceptions. Lets see these keywords in detail one by one.

Throw

It is a keyword which is used to throw the exceptions. It comes in use when we want to throw an exception manually. For example: If password field is not filled by the user then our requirement is throw an exception. It is used in an method and when it is executed then it return to the caller.

Syntax of throw:-

throw new exception_class(" message");

Example of throw:-

public class ExampleThrow {
   static void checkEligibilty( String name, String password){
      if(name.getText().isEmpty && passwordgetText().isEmpty ) {
         throw new ArithmeticException("Password is required");
      }
      else {
         System.out.println("User has a authentication");
      }
   }

   public static void main(String args[]){
     System.out.println("Welcome");
     checkEligibilty(Mam, mam);
     System.out.println("Have a nice day..");
 }
}

Throws

It is also a keyword which is used to throw the exceptions. It looks like throw keyword but have some different funtionality from it. It is used to declare that a method can throw the exception and caller catch the exception if it is unchecked exception.

Public class Example1{  
   void method1() throws ArithmeticException{  
throw new ArithmeticException("Calculation error");
   }  
   void method2() throws ArithmeticException{  
method1();  
   }  
   void method3(){  
try{  
   method2();  
}
catch(ArithmeticException e){
   System.out.println("ArithmeticException handled");
}  
   }  
   public static void main(String args[]){  
Example1 obj=new Example1();  
obj.method3();  
System.out.println("End Of Program");  
   }  
}

Try and Catch

Try and catch are keywords which are used to handle the exception. Catch keyword can't be used single, means without catch.

Some facts about try catch block ,you should know:

  • A try statement can be followed by multiple catch statement.
  • We can use nested try catch statements also.
  • We can't write any statement between try and catch statements.
  • Catch is used only with try but try can be used with finally also.

Syntax of Try and Catch:-

try{
    statement(s)
}
catch (exceptiontype name){
statement(s)
}

Example of try and catch

class JavaException {
 public static void main(String args[]) {
  int div = 0;
  int num = 20;
  try {
   int fraction = num / div;
   System.out.println("This line will not be Executed");
  } catch (ArithmeticException e) {
   System.out.println("In the catch Block due to Exception = " + e);
  }
  System.out.println("End Of Main");
 }
}

Finally

It is a keyword which is used with try or try catch block. After execution of try and catch, finally must be execute. It is also comes in use when we want to avoid the program from halting. Finally block executes always whether an exception is occurred or not.

Syntax of finally block

try {
  statement(s)
 } catch (ExceptiontType name) {
  statement(s)
 } finally {
  statement(s)
 }

Example of finally

class JavaException {
public static void main(String args[]){
try{
        int d = 0;
        int n =20;
        int fraction = n/d;
    }
catch(ArithmeticException e){
    System.out.println("In the catch clock due to Exception = "+e);
  }
  finally{
System.out.println("Inside the finally block");
  }
}
}

These are the five keywords which is used to handle the exceptions in Java. Java is the best option to choose for your career because it is used mostly all of the places whether it is a website or it is any game or it is an application of Android phone. To learn Java in depth you should go for advanced Java training in Rohini, Delhi. And for acquiring such training joining professional Java course in Delhi from a renowned institute is highly recommended. 

If you want to know my recommendation on Java training institute then ADMEC Multimedia Institute would be right place for you. It is one of the prominent web design and web development institutes in Delhi which offers professional diploma and certificate courses in the arena of multimedia designing. 
All the best to be a programmer!!

Featured Post

ADMEC Multimedia Institute Scholarship Program

The ADMEC Multimedia Institute scholarship program aims to select and trained talented non working female, married woman and non married m...