Pull to refresh

How to Use Throw and Throws in Java

Level of difficultyEasy
Reading time2 min
Views1.9K

Exception handling in Java is the most effective way to handle runtime errors occurring in the application. This is used to protect the abnormal flow of the execution of the application and continue the application in normal flow. This is the process of handling runtime errors such as ClassNotFoundException, IOException, etc. The throw and throws keywords are used to handle exceptions in Java.
In this topic, we will learn how to use throw and throws keywords in Java with examples.

Java throw 

It is a keyword which is used to explicitly throw exceptions and customize the exception as per the need in Java.

Syntax of Java throw

throw new exception_class(?message?);

→ The “exception_class”  must be Throwable or a child class of the Throwable.

Example

throw new ArithmeticException("/ by zero");

Example of throw keyword in Java

In this example, we will create a custom exception in Java.

class MyException extends Exception {
public MyException(String s){
 super(s);
    }
}
public class Main {
public static void main(String args[]){
try {
//throw an object of user-defined exception
throw new MyException("SpringJava");
}catch (MyException ex) {
System.out.println("Caught");
System.out.println(ex.getMessage());
        }
    }
}

Output

Caught
SpringJava

Java throws

It is a keyword used to delegate the responsibility of exception handling to the caller method then the caller method is responsible for handling that occurred exception.

Syntax of Java throws

type method_name(parameters) throws exception_list

Examples of Java throws in exception handling 

Example 1
In this example exception is unhandled.

public class SpringJava{
public static void main(String[] args){
Thread.sleep(10000);
System.out.println("Hello SpringJava");
    }
}

Output

/SpringJava.java:3: error: unreported exception InterruptedException; must be caught or declared to be thrown
Thread.sleep(10000);
            ^
1 error

Example 2
In this example, an exception is handled with throws.

public class SpringJava{
public static void main(String[] args)throws InterruptedException{
Thread.sleep(10000);
System.out.println("Hello SpringJava");
    }
}

Output

Hello SpringJava

Example 3
In this example, an exception is handled with the use of throw, throws and try-catch block.

public class SpringJava{
static void demo() throws IllegalAccessException{
System.out.println("inside the demo() method");
throw new IllegalAccessException("Test");
}
public static void main(String args[]){
try {
demo();
}catch (IllegalAccessException e) {
System.out.println("caught in main.");
        }
    }
}

Output

inside the demo() method
caught in the main

FAQ

Q1: What is the purpose of throw and throws in Java?
Ans. A throw keyword is used to throw an exception explicitly. This keyword can throw only one exception at a time. A throws keyword can be used to declare multiple exceptions separated by a comma(,). 
Q2: Can we use throw without try?
Ans. We can use the throws keyword for exception handling without using a try-catch block.
Q3: Why we use throws instead of try catch?
Ans. We can use the throws keyword to declare the exception with the method declaration. It is used to forcibly throw the exception, while the try-catch block handles the exceptions thrown by the code in Java.

Conclusion

In this topic, we learnt how to use throws and throws keywords for exception handling in Java with examples

Tags:
Hubs:
Total votes 3: ↑3 and ↓0+3
Comments0

Articles