throw, throws & finally

  • June 23, 2022
  • Java
legacy classes

The Java keywords for managing exceptions include throw, throws, and finally.

throw keyword:

To throw an exception explicitly in Java, use the throw keyword.

The exception object that is to be thrown is specified. The notice that comes with the exception contains a description of the error. These anomalies could be caused by user inputs, the server, etc.

Java’s throw keyword allows us to throw either verified or unchecked exceptions. It mostly serves as a custom exception thrower.

throw new exception_class("error message");  
Example of throw Exception:
public class Demo {   
    //function to check if a student passed or failed   
    public static void validate(int percent) {  
        if(percent<30) {  
            throw new ArithmeticException("Student Failed");    
        else {  
            System.out.println("Student Passed!!");  
    //main method  
    public static void main(String args[]){  
        //calling the function  

When percent is 33%.

Student Passed!!

When percent is 28%. The exception is successfully handled.

Exception in thread "main" java.lang.ArithmeticException: Student Failed
	at Demo.validate(
	at Demo.main(
throws Keyword:

Declaring an exception in Java requires the throws keyword. The programmer receives a notification that an exception might happen. Therefore, the programmer should provide the exception handling code to retain the program’s usual flow.

The checked exceptions are often handled through exception handling. It is the responsibility of the programmer to check the code before using it if an unchecked exception like NullPointerException occurs.

return_type method_name() throws exception_class_name{  
//method code  
Example of throws Keyword:
// Java program to demonstrate working of throws
class Demo {
    static void fun() throws IllegalAccessException
        System.out.println("Inside fun(). ");
        throw new IllegalAccessException("demo");
    // This is a caller function
    public static void main(String args[])
        try {
        catch (IllegalAccessException e) {
            System.out.println("caught in main.");
Inside fun(). 
caught in main.
finally block:

The finally block in Java is used to run crucial code, such as connection closure, among other things.

Whether an exception is handled or not, the Java finally block is always run. Therefore, regardless of whether an exception happens or not, it contains all the relevant statements that must be printed.

The try-catch block is followed by the finally block.

Example of finally block:
// Java program to demonstrate working of try,
// catch and finally

class Demo {
	public static void main(String[] args)
		int a = 10, b = 5, c = 5, result;
		try {
			result = a / (b - c);
			System.out.println("result" + result);

		catch (ArithmeticException e) {
			System.out.println("Exception caught:Division by zero");

		finally {
			System.out.println("I am in final block");
Exception caught:Division by zero
I am in final block

Note: also read about the Nested try block & try with Resource Statement

Follow Me

If you like my post, please follow me to read my latest post on programming and technology.

Leave a Reply

Your email address will not be published. Required fields are marked *