Java throw and throws Keywords

In Java exception handling, throw keyword is used to explicitly throw an exception from a method or constructor. And throws keyword is used to declare the list of exceptions that may be thrown by that method or constructor.

1. Throw

Let us learn basic things about throw keyword before going deep.

1.1. Syntax

To throw an exception from a method or constructor, use throw keyword along with an instance of exception class.

public void method() {

   //an unexpected event occured
   throw new CustomException("message");
}

1.2. Handling Checked vs Unchecked Exceptions

If we throw an unchecked exception from a method, it is not mandatory to handle the exception or declare in throws clause. For example, NullPointerException is an unchecked exception.

public class JavaExample {

	public static void main(String[] args) {
		method();
	}

	public static void method( ) {
		throw new NullPointerException();
	}
}

But if we throw a checked exception using throw statement, we MUST either handle the exception in catch block or method must explicitly declare it using throws declaration. For example, FileNotFoundException is a checked exception.

public class JavaExample {

  public static void main(String[] args) {
    try {
      method();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }
  }

  public static void method() throws FileNotFoundException {
    throw new FileNotFoundException();
  }
}

In Java, every subclass of Error and RuntimeException is an unchecked exception. A checked exception is everything else under the Throwable class.

1.3. Exception Propagation

An exception propagates from method to method, up the call stack, until it’s caught. So if a() calls b(), which calls c(), which calls d(), and if d() throws an exception, the exception will propagate from d to c to b to a, unless one of these methods catches the exception.

The search for exception handler begins with the method in which the error occurred and proceeds through the call stack in the reverse order in which the methods were called, as described above.

When an appropriate handler (catch block) is found, the runtime system passes the exception to the handler. If no exception handler is found then exception reaches to JVM’s default exception handler which prints the exception details to logs and terminates the application.

2. Throws

Java throws keyword is used to declare a list of exceptions that may occur during the method execution.

2.1. Syntax

To declare the list of exceptions, use throws keyword along with exception class names.

public static void method( ) throws FileNotFoundException, ConnectionException {
	//code
}

2.2. Allowed to throw checked and unchecked exceptions

We can declare both types of exceptions using throws clause i.e. checked and unchecked exceptions. But the method calling the given method must handle only checked exceptions. Handling unchecked exceptions is optional.

public class JavaExample {

  public static void main(String[] args) {
    try {
      //Can skip handling of NullPointerException (unchecked exception)
      method();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }
  }

  public static void method() throws NullPointerException, FileNotFoundException {
    //code
  }
}

3. Difference between throw and throws in Java

  • throw keyword is used to throw a single exception explicitly from any method or constructor while throws keyword is used in method and constructor declaration, denoted which exception can possibly be thrown by this method.
  • throw is followed by an instance of exception class while throws is followed by exception class name.
  • throw is used within the method and constructor where as throws is used with the method and constructor signature.
  • We can throw only single exceptions using throw, but we can declare multiple exceptions using throws one of which may or may not throw by method.
  • A checked exception is propagated to the caller method, while unchecked exceptions are not propagated and thus may not require explicit exception handling.
  • Using throw keyword we can also break a switch statement or a loop without using break keyword which cannot be performed using throws.

Happy Learning !!

Comments

Subscribe
Notify of
guest
4 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode