I need to modify the given method. 'RunTimeException' is UNchecked... It should throw a checked exception:
public class Main {
public static void method() {
throw RuntimeException;
}
public static void main(String[] args) {
try {
method();
} catch (RuntimeException e) {
System.out.println("RuntimeException");
} catch (Exception e) {
System.out.println("Exception");
}
}
}
I got error:
Compilation error
Main.java:4: error: cannot find symbol
throw RuntimeException;
^
symbol: variable RuntimeException
location: class Main
1 error
I strongly suggest reading tutorial here https://docs.oracle.com/javase/tutorial/essential/exceptions/runtime.html .
Here is the excerpt which relates to your queston
Generally speaking, do not throw a RuntimeException or create a subclass of RuntimeException simply because you don't want to be bothered with specifying the exceptions your methods can throw.Here's the bottom line guideline: 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 technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.