throw

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 7

Exception Handling

Exception Handling is one of the effective means to handle runtime errors so that the
regular flow of the application can be preserved. Java Exception Handling is a
mechanism to handle runtime errors such as ClassNotFoundException, IOException,
SQLException, RemoteException, etc.
The throw keyword in Java is used to explicitly throw an exception from a method or
any block of code. We can throw either checked or unchecked exception. The throw
keyword is mainly used to throw custom exceptions.
throw Instance
Example:
throw new ArithmeticException("/ by zero");
The flow of execution of the program stops immediately after the throw statement is executed and the nearest enclosing try block is checked to see if it has a catch
statement that matches the type of exception. If it finds a match, controlled is transferred to that statement otherwise next enclosing try block is checked, and so on.
If no matching catch is found then the default exception handler will halt the program.

//Java program that demonstrates the use of throw


class ThrowExcep {
static void fun()
{
try {
throw new NullPointerException("demo");
}
catch (NullPointerException e) {
System.out.println("Caught inside fun().");
throw e; // rethrowing the exception
}
}
public static void main(String args[])
{
try {
fun();
}
catch (NullPointerException e) {
System.out.println("Caught in main.");
}
}
}
Caught inside fun().
Caught in main.
To prevent this compile time error we can handle the exception in two ways:
By using try catch
By using the throws keyword

// Java program to illustrate error in case


// of unhandled exception
class tst {
public static void main(String[] args)
{
Thread.sleep(10000);
System.out.println("Hello Geeks");
}
}
error: unreported exception InterruptedException; must be caught or declared to be thrown
Explanation
In the above program, we are getting compile time error because there is a chance of exception if the
main thread is going to sleep, other threads get the chance to execute the main() method which will
cause InterruptedException.
// Java program to illustrate throws
class tst {
public static void main(String[] args)
throws InterruptedException
{
Thread.sleep(10000);
System.out.println("Hello Geeks");
}
}
Hello Geeks
User-defined Custom Exception in Java
An exception is an issue (run time error) that occurred during the execution of a program.
When an exception occurred the program gets terminated abruptly and, the code past the line
that generated the exception never gets executed.
Java provides us the facility to create our own exceptions which are basically derived classes
of Exception. Creating our own Exception is known as a custom exception or user-defined
exception. Basically, Java custom exceptions are used to customize the exception according to
user needs.
// A Class that represents use-defined exception
class MyException extends Exception {
public MyException(String s)
{
// Call constructor of parent Exception
super(s);
}
}
// A Class that uses above MyException
public class Main {
// Driver Program
public static void main(String args[])
{
try {
// Throw an object of user defined exception
throw new MyException("GeeksGeeks");
}
catch (MyException ex) {
System.out.println("Caught");
// Print the message from MyException object
System.out.println(ex.getMessage());
}
}
}

You might also like