Wednesday 19 August 2015

Exception Handling Keywords in Java

Exception Handling Keywords in Java

Exception Handling is mainly used to handle the checked exceptions.
If there is any unchecked exception such as NullPointerException, it is programmer’s fault that he is not performing check-up before the code being used.

throw:
When we want to create an explicit exception object and then throw it to halt the normal processing of the program. throw keyword is used to throw exception to the runtime to handle it.

public class ThrowDemo {
      
    /**This method throwing the unchecked exception/runtime exception.
     * So there is no need to use throws keyword with this method.*/
    static void validate(int age) { 
           if(age<18) {
                 throw new ArithmeticException("not valid"); 
           } else {
                 System.out.println("welcome to vote");
           }
    }
    public static void main(String args[]){ 
           validate(13); 
           System.out.println("rest of the code..."); 
    } 
}

throws:
When we throw any checked exception without handling it, then we need to use throws keyword  to let inform the caller program that checked exception is not handles in method that called.
The caller method might handle these exceptions or propagate it to its caller method using throws keyword.

We can provide multiple exceptions in the throws clause and it can be used with main() method also.

Advantage of Java throws keyword
Now Checked Exception can be propagated (forwarded in call stack) using throws keyword. It provides information to the caller of the method about the exception.

If you are calling a method that declares an checked exception, you must either caught or declare the exception (throws).

import java.io.IOException; 
class ThrowsDemo { 
     void methodThrows() throws IOException{
           //checked exception - either throws or handle in try catch block.
           throw new IOException("methodThrows : device error"); 
     }

     // throws use for IOException propogation
     void method() throws IOException {
           // this will throw IOException checked exception
           methodThrows();
     } 
      
     void methodHandle() { 
           try
                // this will throw IOException checked exception
                method();
           } catch(Exception e) {
                 // handle the IOException checked exception
                 System.out.println("methodHandle : exception handled");
           } 
     }
      
     public static void main(String args[]){ 
           ThrowsDemo obj=new ThrowsDemo(); 
           obj.methodHandle(); 
           System.out.println("normal flow..."); 
     } 
}

throw and throws

throw
throws

Java throw keyword is used to explicitly throw an exception.


Java throws keyword is used to declare an exception.

Checked exception cannot be propagated using throw only.

Checked exception can be propagated with throws.


Throw is followed by an instance.
void methodThrows() throws IOException {
  // instance
  throw new IOException("methodThrows");
}


Throws is followed by class.
// class
void method() throws IOException

Throw is used within the method.


Throws is used with the method signature.


We cannot throw multiple exceptions.

We can declare multiple exceptions e.g.
public void method () throws
IOException, SQLException.


try-catch:
We use try-catch block for exception handling in our code. try is the start of the block and catch is at the end of try block to handle the exceptions. Multiple catch blocks with a try and try-catch block can be nested also.

catch block requires a parameter that should be of type Exception.

finally:
finally block is optional and can be used only with try-catch block.

Since exception halts the process of execution, we might have some resources open that will not get closed, so we can use finally block.
finally block gets executed always, whether exception occurs or not. It will not executed when the System halt.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...