Why can't I throw an exception from a method
Asked Answered
A

4

5

I am new at Java and am experiencing a bit of a problem with throwing exceptions. Namely, why is this incorrect

public static void divide(double x, double y) {
    if (y == 0){
        throw new Exception("Cannot divide by zero."); 
        // Generates error message that states the exception type is unhanded 
    }
    else
        System.out.println(x + " divided by " + y + " is " + x/y);
        // other code follows
}

But this OK?

public static void divide(double x, double y) {
    if (y == 0)
        throw new ArithmeticException("Cannot divide by zero.");
    else
        System.out.println(x + " divided by " + y + " is " + x/y);
        // other code follows
}
Alignment answered 23/11, 2013 at 0:32 Comment(1)
Blame a guy named Goodenough, ca 1974.Mandell
D
10

An ArithmeticException is a RuntimeException, so it doesn't need to be declared in a throws clause or caught by a catch block. But Exception isn't a RuntimeException.

Section 11.2 of the JLS covers this:

The unchecked exception classes (§11.1.1) are exempted from compile-time checking.

The "unchecked exception classes" include Errors and RuntimeExceptions.

Additionally, you'll want to check if y is 0, not if x / y is 0.

Danyelldanyelle answered 23/11, 2013 at 0:33 Comment(1)
I would like to generate a NotSerializableException when an object is being set up for Serialization - by checking if(someObj instanceof Serializable) That is not possible then?Canker
B
3

You need to add a throws in the method signature only for checked exceptions. For example:

public static void divide(double x, double y) throws Exception {
 ...
}

Since ArithmeticException extends RuntimeException, there's no need of a throws in the second example.

More info:

Bettyebettzel answered 23/11, 2013 at 0:33 Comment(0)
C
0

Methods that throw exceptions in Java must delcare it in the method signature

public static void divide(double x, double y) throws Exception

Without the declaration your code will not compile.

There is a special subset of exceptions that extend the RuntimeException class. These exceptions do not need to be declared in the method signature.

ArithmeticException extends a RuntimeException

Crust answered 23/11, 2013 at 0:36 Comment(0)
M
0

ArithmeticException is an unchecked exception, since it extends RuntimeException. Exception, by contrast, is an checked exception type, as it does not extend either RuntimeException or Error.

Checked exceptions must be declared in the throws clause of the method signature of any method that can throw them. Unchecked exceptions do not.

Using your example, adding throws Exception will be enough to get the method to compile. Note though that any calling code would then need to handle the exception, either by catching it or declaring it as being thrown.

public static void divide(double x, double y) throws Exception {
    if (y == 0){
        throw new Exception("Cannot divide by zero."); 
        // Generates error message that states the exception type is unhanded 
    }
    else
        System.out.println(x + " divided by " + y + " is " + x/y);
        // other code follows
}

This is documented in the Java Language Specification (JLS):

11.1.1. The Kinds of Exceptions

[…]

The unchecked exception classes are the run-time exception classes and the error classes.

The checked exception classes are all exception classes other than the unchecked exception classes. That is, the checked exception classes are Throwable and all its subclasses other than RuntimeException and its subclasses and Error and its subclasses.

[…]

11.2. Compile-Time Checking of Exceptions

The Java programming language requires that a program contains handlers for checked exceptions which can result from execution of a method or constructor (§8.4.6, §8.8.5). This compile-time checking for the presence of exception handlers is designed to reduce the number of exceptions which are not properly handled. For each checked exception which is a possible result, the throws clause for the method or constructor must mention the class of that exception or one of the superclasses of the class of that exception (§11.2.3).

[…] The unchecked exception classes (§11.1.1) are exempted from compile-time checking.

Messene answered 7/10, 2023 at 19:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.