à®®ேலுà®®் பல புதிய டெக்னாலஜி பற்à®±ி தெà®°ிந்து கொள்ள, எங்கள் "டெக் டுப் தமிà®´் நாடு" யூட் டூஃப் சேனலை SUBSCRIBE செய்து கொள்ளவுà®®்.

Breaking

Sunday, 8 October 2017

Exception Handling in JAVA 7


Exception Handling in JAVA 7

Java throw keyword

The Java throw keyword is used to explicitly throw an exception.Only object of Throwable class or its sub classes can be thrown. Program execution stops on encountering throw statement, and the closest catch statement is checked for matching type of exception.

Syntax:

throw ThrowableInstance

Creating Instance of Throwable class:

There are two possible ways to get an instance of class Throwable,

1. Using a parameter in catch block..

2. Creating instance with new operator.

5. Only the object of Throwable class or its subclasses can be thrown.

new NullPointerException("test");

Example demonstrating throw Keyword

class HelloWorld
{
 static void validate(int age) 
 {
  if(age < 18)
  {
   try
   {
    throw new ArithmeticException("NOT Valid");
   }
   catch(ArithmeticException e)
   {
    System.out.println("Age Invalid Exception Caught");
   }
  }
  else
  {
   System.out.println("Welcome to vote"); 
  } 
 }

 public static void main(String args[])
 {
  validate(13); 
 }
}
Output : Age Invalid Exception Caught

In the above example the validate() method throw an instance of ArithmeticException, which is successfully handled using the catch statement.

Java throws keyword

Any method capable of causing exceptions must list all the exceptions possible during its execution, so that anyone calling that method gets a prior knowledge about which exceptions to handle. A method can do so by using the throws keyword.

Syntax :

return_type  method_name ( parameter_list ) throws exception_list
{
       //definition of method
}

NOTE : It is necessary for CheckedExceptions (Compile-time) and not necessary for RuntimeException (Run-time) and Error, or any of their subclass.

Java throws example

Let's see the example of java throws clause which describes that checked exceptions can be propagated by throws keyword.

There are two cases:

1. Case 1 : You caught the exception i.e. handle the exception using try/catch.

2. Case 2 : You declare the exception i.e. specifying throws with the method.

Case1: You handle the exception

In case you handle the exception, the code will be executed fine whether exception occurs during the program or not.

class test{  
 void method()throws IOException{  
  throw new IOException("device error"); //checked exception 
 }  
}  
public class Testthrows{  
   public static void main(String args[]){  
    try{  
     M m=new M();  
     m.method();  
    }catch(Exception e){System.out.println("Exception handled");}     
  }  
}  
Output: Exception handled

Case2: You declare the exception

A) In case you declare the exception, if exception does not occur, the code will be executed fine.

B) In case you declare the exception if exception occures, an exception will be thrown at runtime because throws does not handle the exception.

class test{  
 void method()throws IOException{  
  System.out.println("Device Operation Performed");  
 }  
}  
class Testthrows{  
   public static void main(String args[]) throws IOException{ //declare exception  
     M m=new M();  
     m.method();  
  }  
}  

Output :
A) Device Operation Performed
B) Runtime Exception

NOTE : If you are calling a method that declares an exception, you must either caught or declare the exception.

Difference between throw and throws in Java:

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.Throws is followed by class.
Throw is used within the method.Throws is used with the method signature.
You cannot throw multiple exceptions.You can declare multiple exceptions e.g.
public void method()throws IOException,SQLException.

No comments:

Post a Comment

Hai , Post your comment . (required, Bugs, Errors )

You need to see


SUBSCRIBE NOW



Backlinks : Website Design Company in Coimbatore
Website Design Company in Tamil nadu
Website Design Company in Karur
Website Design Company in Erode
Website Design Company in ooty
Website Design Company in theni
Website Design Company in thoothukudi
Website Design Company in Chennai
Website Design Company in Sathy
Website Design Company in Coimbatore Tamilnadu
Cheap Website Design Company in Coimbatore
Adsense Website Design Company in Coimbatore
ecommerce Website Design Company in Coimbatore
school Website Design Company in Coimbatore
Academy Website Design Company in Coimbatore
Test Website Design Company in Coimbatore
Best Website Design Company in Coimbatore
Adsense Website Design Company in Coimbatore
Adsense Website Design Company in Tamil nadu
Adsense Website Design Company in Karur
Adsense Website Design Company in Erode
Adsense Website Design Company in ooty
Adsense Website Design Company in theni
Adsense Website Design Company in thoothukudi
Adsense Website Design Company in Chennai
Adsense Website Design Company in Sathy
Free SEO Tools online
Free SEO Tools online Website
Free SEO Tools online Plagiarism
Free SEO Tools online article rewriter tool
Free SEO Tools online article reading and writing criticism
Free SEO Tools online seo backlink
Free SEO Tools online Free backlink maker
Free SEO Tools online seo backlink strategy
Free SEO Tools online content writting
360 help SEO Tools online Plagiarism checker
360 help SEO Tools online seo backlink
360 help SEO Tools online Free backlink maker
360 help SEO Tools online content writting
360 help SEO Tools online article rewriter tool
360 help SEO Tools
360 help SEO Tools India
360 help SEO Tools Tamil
360 help SEO Tools website