A Java exception is an object that describes an exceptional (that is,
error) condition that has occurred in a piece of code. When an exceptional
condition arises, an object representing that exception is created and thrown in the method that caused the
error. That method may choose to handle the exception itself, or pass it on.
Either way, at some point, the exception is caught
and processed. Exceptions can be generated by the Java run-time system, or they
can be manually generated by your code
Program statements that you want to monitor for exceptions are contained
within a try block. If an exception
occurs within the try block, it is
thrown. Your code can catch this exception (using catch) and handle it. System-generated exceptions are automatically
thrown by the Java run-time system.
To manually throw an exception, use the keyword throw. Any exception that is thrown out of a method must be
specified as such by a throws
clause. Any code that absolutely must be executed after a try block completes is put in a finally block.
A general form of an
exception-handling block: try {
// block of code to monitor for errors
}
catch (ExceptionType1 exOb) {
// exception handler for ExceptionType1
}
// ...
finally {
// block of code to be executed after try block
ends
}
Here, ExceptionType is the
type of exception that has occurred.
It is possible for your program to throw an exception
explicitly, using the throw
statement. The general form of throw
is shown here:
throw ThrowableInstance;
Here, ThrowableInstance must
be an object of type Throwable or a
subclass of Throwable. Primitive
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 in a catch
clause, or creating one with the new
operator.
public class throwexample {
static void add(){
//int a=0,b=25;
try{
throw new NullPointerException("demo"); } catch(NullPointerException
e){
System.out.println(" Caught inside demoproc() "); throw e;
}
}
public static void main(String args[])
{
try{
add();
}
catch (NullPointerException e){ System.out.println("Recaught"+e);
}
}
}
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. Athrows
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
type method-name(parameter-list) throws exception-list
{
// body of method
}
package day1;
public class throwsexceptionexample {
static void throwone() throws IllegalAccessException { System.out.println("Inside
throws");
throw new IllegalAccessException("Demo");
}
public static void main(String args[])
{
try{
throwone();
}
catch (IllegalAccessException e){ System.out.println("caught"+e);
}
}
}
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2026 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.