Saturday, December 24, 2011

Throws Keyword in JAVA

Throws Keyword :

"throws " is a keyword defined in the java programming language. Keywords are basically reserved words which have specific meaning relevant to a compiler in java programming language likewise the throw keyword indicates the following :
-- The throws keyword in java programming language is applicable to a method to indicate that the method raises particular type of exception while being processed.-- The throws keyword in java programming language takes arguments as a  list of the objects of type java.lang.Throwables class.
-- when we use the throws with a method it is known as ducking. The  method calling a method with a throws clause is needed to be enclosed within the try catch blocks.

Example

import java.io.*;
import java.lang.*;
public class throwsWord
{
   public static void main(String args[])
   {
      int a,b;
      try
      {
            a=Integer.parseInt(args[0]);
            if(a!=0)
            {
                 b=100/a;
            }
            else
            {
                 throw new ArithmeticException("Error");
            }
            System.out.println("division:" + b);
      }
      catch(ArithmeticException e)
      {
           System.out.println("Division by 0..");
           throw e;
      }
   }
}

No comments:

Post a Comment