Mikkel
Mikkel

Reputation: 512

When is it necessary to use "throws" in the method signature?

I am not fully understanding when I have to use the "throws" keyword. Consider this code:

import java.util.InputMismatchException;
import java.util.Scanner;

public class Main {

    public static void otherMethod() throws InputMismatchException {
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();
        System.out.println(num);
    }

    public static void main (String[] args) {
        try {
            otherMethod();
        } catch(InputMismatchException e) {
            System.out.println("Please input an integer");
        }
    }
}

If you input anything that is not an integer, it would result in an InputMismatchException. This exception is passed on to the main method. However, I found that the "throws InputMismatchException" part of the otherMethod() doesn't matter at all, and the exception is still handled properly by the main method without it. Without this part, the code would look like this:

import java.util.InputMismatchException;
import java.util.Scanner;

public class Main {

    public static void otherMethod() {
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();
        System.out.println(num);
    }

    public static void main (String[] args) {
        try {
            otherMethod();
        } catch(InputMismatchException e) {
            System.out.println("Please input an integer");
        }
    }
}

I am wondering when and why the "throws" keyword is used, and what it actually does.

Upvotes: 1

Views: 346

Answers (4)

Encaitar
Encaitar

Reputation: 398

Java has checked exceptions and unchecked exception. Unchecked exceptions don't have to be declared if they can be thrown from a method. Checked exceptions must be caught and dealt with or explicitly declared to be thrown by the method.

Checked exceptions are ones that should be anticipated and be able to be dealt with or recovered from. Unchecked or runtime exceptions represent exceptional circumstances you are not expected to have anticipated or be able to recover from.

From the official docs:

"If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception."

The java docs walk you through the class hierarchy in Java

java.lang.Object
  java.lang.Throwable
    java.lang.Exception
      java.lang.RuntimeException

Where any exception that inherits from RuntimeException is an unchecked exception but exceptions that inherit directly from Exception are checked. There are also Errors which are slightly different.

Upvotes: 0

Mạnh Quyết Nguyễn
Mạnh Quyết Nguyễn

Reputation: 18235

There are two kinds of exception in Java: checked exception and unchecked exception.

Checked exceptions require the programmer to explicit catch or declare throws in method signature. Meanwhile, unchecked exception do not require to explicit catch.

Unchecked exceptions are exception who is or is subclass of RuntimeException or Error. Checked exception are everything left.

Your InputMismatchException extends from NoSuchElementException, and NoSuchElementException extends from RuntimeException, so it's not required for you to declare throws or catch it.

Upvotes: 0

xingbin
xingbin

Reputation: 28269

InputMismatchException extends from NoSuchElementException, which extends from RuntimeException, and:

RuntimeException and its subclasses are unchecked exceptions. Unchecked exceptions do not need to be declared in a method or constructor's throws clause if they can be thrown by the execution of the method or constructor and propagate outside the method or constructor boundary.

Upvotes: 3

RyanJ
RyanJ

Reputation: 158

You can use the throws keyword when you know an exception could be thrown, but you do not want to handle it in any way.

Upvotes: 0

Related Questions