Wednesday, February 5, 2014

Exceptions in Java - Throwing

In my later post I talked about try-catch block. In this post I gonna talk more about exceptions and Throw and Throws clauses. Before learn them, it is better to get an idea about types of exceptions. 

Checked exceptions


  • Exceptions which are checked at the compilation time are called "Checked exceptions".
  • We must handle these exceptions using "try - catch" block.
  • If there is no try-catch block, it will return compile time error.
  • All the exceptions which are belongs to exception class except Run-time exceptions are known as checked exceptions. 

Checked exceptions

Unchecked exceptions


  • Most of the unchecked exceptions are arisen due to our programming errors.
  • All the sub classes of RuntimeExceptions are called unchecked exceptions. 
  • These exceptions are not checked at the compile time and does not mandatory to handle by try catch block.
  • In above picture you can see what are the unchecked exceptions.

public class UncheckedTest {
 
 static String name=null;
 public static void main(String args[]){
  
  System.out.println(name.length());
 }
}


This will give a NullPointerException.


NullPointerException

Throws and Throw clauses


This is the main section of this post. When exception handling this is an important thing. You need to understand how to use these clauses and what is the different between them. When I use try-catch block it is doing catching exceptions which are generated by the JVM. In this section we are gonna talk about throwing exceptions.


What is the different between Throw and Throws ?


This is a common interview question and it is also messy question. I also burned a lot of time to get an idea for this matter. This is what I got.

When you use Throws keyword in a method, it means that this method can throw exceptions. If you want to call this method,  You have to think about its exception throwing. Simply I can say Throws keyword is used to declare an exception. 
I'm telling what you can do


When you use Throw keyword, it is not about declaration, it is really throw an exception. Other way it is like commanding to handle exceptions.  

Hey, you should do it right ?


Why we need to Throw/Throws exceptions ?


When you working on a large project, there can be set of codes. So you have to handle more exceptions. It is easy to handle all exceptions in your own exception class. That is why we are using throwing. In this case, if there is an exception, we throw it to exception class that you created. Then it will be handled.


Throws clause 


  • This clause is used to declare  an exception.
  • If a method generates an exception that it does not handle, it must be declared in this clause. 
  • You also can throw multiple exceptions. You should use commas to separate them. 
  • Errors and run-time exceptions don't need to be specified if you using throws clause. But all other types should be declared.  

You can try following example step by step.

Step 01

public class ThrowsTest {
 public static void main(String args[]){
  
  int marks[] = new int[2];
  System.out.println(marks[5]);
 }
}


This will give you an ArrayIndexOutOfBoundsException.

Step 02

I add these two lines of code into static method called "display( )" and call it in the main method( I used static keyword to make it easy to access, you can change it anyway ).

public class ThrowsTest {
 public static void main(String args[]){
  display();
 }

 static void display() {
  int marks[] = new int[2];
  System.out.println(marks[5]);
 }
}


Step 03

Then I add throws clause after the display() method and surround display() in main method by try-catch block. In catch block I have used my own output message.

public class ThrowsTest {
 public static void main(String args[]){
  try {
   display();
  } catch (ArrayIndexOutOfBoundsException e) {
   System.out.println("There is an error.");
  }
 }

 static void display() 
 throws ArrayIndexOutOfBoundsException{
  int marks[] = new int[2];
  System.out.println(marks[5]);
 }
}


This is the best way to use throws clause. Anyway you can change it as you wish. You can use arguments also and there can be many exceptions to throw. With throw clause we can have checked or unchecked exceptions.

E.g.:

void display() throws NullPointerException,ArrayIndexOutOfBoundsException{
      //your code
}


Throw clause


  • This is used to throw an exception clearly.
  • The throw statement requires a single argument, it is throwable object.
  • All the sub classes of throwable class, can be used as throwable objects.

E.g.: throw ArithmeticException( );

Try this example to understand about throw clause.


class ThrowTest{
 public static void main(String args[]){
  try{
   int marks[] = new int[2];
   System.out.println(marks[5]);
   
   throw new ArrayIndexOutOfBoundsException();
   
  }catch(ArrayIndexOutOfBoundsException e){
   System.out.println("Exception caught");
  }
  System.out.println("After");
 }
}


Important 


You cannot have any statements after the throw clause. Below picture shows you the error message that you can see in Eclipse.


you cannot have statements after the throw clause


I think you got an idea about throw and throws clauses. Hope you enjoy, will be back with new post of Java...