Wednesday, 16 November 2011

throw and throws


throw

Normally the exceptions are caughted that are thrown by the Java run-time system. However, it is possible for your program to throw an exception explicitly, using the throw statement.
syntax
throw ThrowableInstance;

Here, ThrowableInstance must be an object of type Throwable or a subclass of Throwable. Simple types, such as int or char, as well as non-Throwable classes, such as String and Object, cannot be used as exceptions. There are two ways you can obtain a Throwable object: using a parameter into a catch clause, or creating one with the new operator. The flow of execution stops immediately after the throw statement; any subsequent statements are not executed. The nearest enclosing try block is inspected to see if it has a catch statement that matches the type of the exception. If it does find a match, control is transferred to that statement. If not, then the next enclosing try statement is inspected, and so on. If no matching catch is found, then the default exception handler halts the program and prints the stack trace.

Example


class ThrowDemo
  {
     static void demoproc()
     {
       try
       {
        Throw new NullPointerException("demo");
       }
       catch(NullPointerException e)
       {
  System.out.println("Caught inside                                         demoproc.");
  throw e; // re-throw the exception
      }
   }

 public static void main(String args[])
 {
    try
     {
        demoproc();
     }
     catch(NullPointerException e)
     {
      System.out.println("Recaught: " + e);
    }
  }
}

throws

If a method is capable of causing an exception that it does not handle, it must specify this behavior so that callers of the method can guard themselves against that exception.This is done by including a throws clause in the method’s declaration. A throws clause lists the types of exceptions that a method might throw. This is necessary for all exceptions, except those of type Error or RuntimeException, or any of their subclasses.All other exceptions that a method can throw must be declared in the throws clause. If  they are not, a compile-time error will result.

This is the general form of a method declaration that includes a throws clause:
type method-name(parameter-list) throws exception-list
{
// body of method
}
Here, exception-list is a comma-separated list of the exceptions that a method can throw.
 
public void doSomething() throws IOException, FileNotFoundException
{
            // code
}
As long as the superclass of any declared exception is also declared thrown, it doesn't have any effect on the code to include the throwing of the subclass. In the example above it has no real effect that FileNotFoundException is declared thrown when IOException is also declared. When you catch IOException you also catch FileNotFoundException. It is still possible to handle the two exceptions with each their own catch-block as shown earlier, even if only the superclass is declared thrown. 

Example,


class ThrowsDemo
{
static void throwOne() throws IllegalAccessException
{
  System.out.println("Inside  throwOne.");
  throw new  IllegalAccessException("Example of Throws");
}
    public static void main(String args[])
    {
    try
    {
      throwOne();
    }
    catch (IllegalAccessException e)
    {
      System.out.println("Caught " + e);
    }
  }
}

No comments:

Post a Comment