unchecked-exception

Is there an advantage to declaring that a method throws an unchecked exception?

狂风中的少年 提交于 2019-11-29 09:29:19
If I have a method which throws an unchecked exception, e.g.: void doSomething(int i) { if (i < 0) throw new IllegalArgumentException("Too small"); // ... } is there any advantage to explicitly declaring that the method throws the exception, i.e. void doSomething(int i) throws IllegalArgumentException { if (i < 0) throw new IllegalArgumentException("Too small"); // ... } as opposed to (or in addition to) describing the behaviour in javadoc: /** * This method does something useful. * @param i some input value * @throws IllegalArgumentException if {@code i < 0} */ void doSomething(int i) { if (i

Should unchecked exceptions be caught and dealt with?

送分小仙女□ 提交于 2019-11-29 07:55:45
I have been reading many posts about exceptions lately and I have a question whether unchecked exceptions should be caught. I have read that if you want your application to recover from an error you use checked exceptions. However if you can't deal with the checked exception you either wrap it into another checked exception so you can pass it to another layer; for instance you wrap an SqlException , or you throw an unchecked exception. However, should you catch unchecked exceptions? Are unchecked exceptions ideally programming errors that you don't check for? Should they just bubble up from

Why is catching checked exceptions allowed for code that does not throw exceptions?

夙愿已清 提交于 2019-11-28 18:12:53
In Java, methods that throw checked exceptions ( Exception or its subtypes - IOException, InterruptedException, etc) must declare throws statement: public abstract int read() throws IOException; Methods that do not declare throws statement can't throw checked exceptions. public int read() { // does not compile throw new IOException(); } // Error: unreported exception java.io.IOException; must be caught or declared to be thrown But catching checked exceptions in safe methods is still legal in java: public void safeMethod() { System.out.println("I'm safe"); } public void test() { // method

Is there an advantage to declaring that a method throws an unchecked exception?

社会主义新天地 提交于 2019-11-28 02:52:42
问题 If I have a method which throws an unchecked exception, e.g.: void doSomething(int i) { if (i < 0) throw new IllegalArgumentException("Too small"); // ... } is there any advantage to explicitly declaring that the method throws the exception, i.e. void doSomething(int i) throws IllegalArgumentException { if (i < 0) throw new IllegalArgumentException("Too small"); // ... } as opposed to (or in addition to) describing the behaviour in javadoc: /** * This method does something useful. * @param i

Why is catching checked exceptions allowed for code that does not throw exceptions?

霸气de小男生 提交于 2019-11-27 11:07:43
问题 In Java, methods that throw checked exceptions (Exception or its subtypes - IOException, InterruptedException, etc) must declare throws statement: public abstract int read() throws IOException; Methods that do not declare throws statement can't throw checked exceptions. public int read() { // does not compile throw new IOException(); } // Error: unreported exception java.io.IOException; must be caught or declared to be thrown But catching checked exceptions in safe methods is still legal in

Difference between Unchecked exception or runtime exception

拜拜、爱过 提交于 2019-11-27 03:18:39
This was an interview question. What is the main difference between unchecked exception and error as both are not caught? They will terminate the program. Pascal Thivent As stated by their name, unchecked exceptions are not checked at compile-time which means that the compiler doesn't require methods to catch or to specify (with a throws ) them. Classes belonging to this category are detailed in the section 11.2 Compile-Time Checking of Exceptions of the JLS: The unchecked exceptions classes are the class RuntimeException and its subclasses, and the class Error and its subclasses . All other

Difference between Unchecked exception or runtime exception

坚强是说给别人听的谎言 提交于 2019-11-26 10:25:25
问题 This was an interview question. What is the main difference between unchecked exception and error as both are not caught? They will terminate the program. 回答1: As stated by their name, unchecked exceptions are not checked at compile-time which means that the compiler doesn't require methods to catch or to specify (with a throws ) them. Classes belonging to this category are detailed in the section 11.2 Compile-Time Checking of Exceptions of the JLS: The unchecked exceptions classes are the

Understanding checked vs unchecked exceptions in Java

半世苍凉 提交于 2019-11-25 21:37:34
问题 Joshua Bloch in \" Effective Java \" said that Use checked exceptions for recoverable conditions and runtime exceptions for programming errors (Item 58 in 2nd edition) Let\'s see if I understand this correctly. Here is my understanding of a checked exception: try{ String userInput = //read in user input Long id = Long.parseLong(userInput); }catch(NumberFormatException e){ id = 0; //recover the situation by setting the id to 0 } 1. Is the above considered a checked exception? 2. Is