Exemption from checked Java exceptions

I read about unverified and verified questions, none of the online resources were really clear about the difference and when to use both.

From what I understand, both of them fall at runtime, both of them represent program states that are outside the expected boundaries of the logic, but checked exceptions should be clearly caught, but not marked - not.

My question is: suppose for an argument I have a method that divides two numbers

double divide(double numerator, double denominator) { return numerator / denominator; } 

and a method requiring somewhere divison

 void foo() { double a = divide(b, c); } 

Who is responsible for verifying that the value of the denominator is zero, and whether or not to exclude (by ignoring Java's built-in division check)?

So whether the divide method will be declared as is or how

 double divide(double numerator, double denominator) throws DivideByZeroException { if(denominator == 0) throw DivideByZeroException else ... } void foo() { try{ double a = divide(b, c); } catch(DivideByZeroException e) {} } 

or without a checked exception, as is:

 double divide(double numerator, double denominator) { if(denominator == 0) throw DivideByZeroException else ... } void foo() { if(c != 0) double a = divide(b, c); } 

and let foo make the division a null check?

This problem originally arose in a mathematical program that I wrote, in which users entered numbers and logical classes performed by calculations. I was never sure if the GUI should immediately check for incorrect values, or if the internal logic should catch them during the calculation and throw exceptions.

+6
java checked exception throw unchecked
source share
6 answers

Interesting topic!

After reading and trying many ways to deal with errors in general and exceptions, I learned to distinguish between programmer errors and expected errors.

Programmer errors should never be caught, but rather a failure (!) Early and hard. A programmer's error is associated with a logical error, and the root cause must be fixed.

Expected errors should always be caught. Also, when an expected error is caught, a message should be displayed to the user. This is important. If the expected error should not display an error, it is better to check whether the method will throw, and not let it throw.

So, as applied to your example, I would think: "How should this look for the user?"

  • If the error message should be displayed (in the output of the browser, console, in the message box), I would select an exception and catch it as close as possible to the user interface and display an error message.
  • If the error message should not be displayed, I would check the input and not select.

On the side: I never throw a DivideByZeroException and a NullPointerException - I let the JVM throw it for me. In this case, you could brew create your own exception class or use a suitable built-in exception.

+7
source share

My favorite discussion of the differences in philosophy between checked and unchecked exceptions in Java:

http://www.javapractices.com/topic/TopicAction.do?Id=129

+1
source share

Java exceptions are checked only by the compiler, however, java developers decided to divide them into several categories, mainly with the extension of the superclass

  • java.lang.Exception - known as checked exceptions
  • java.lang.RuntimeException , known as excluded exceptions - as a bonus java.land.RuntimeException extends java.lang.Exception (to facilitate handling in catch blocks and more)
  • java.lang.Error - errors that are also uncontrolled and rarely required for processing user space by code, but their knowledge and their variance are the main plus. These include (the most famous): binding error, stackoverflow, out of memory, approval errors
  • java.lang.Throwable - Checked! and the mother of all exceptions, few need to directly subclass it, but some for unknown reasons

So about the need to declare exceptions and take care of the correct distribution (only at the compilation level), uncontrolled ones are automatically distributed, and the developer should not provide processing if necessary.

Validated ones are usually expected and require additional verbosity in java, already fluffy code.

Worst practices include: catch (Throwable t){} , for many reasons, usually errors are not handled if necessary, and most errors usually cause thread death anyway.

+1
source share

Assume that the checked exception is selected as a result of a mathematical operation. For example, division (according to your message).
This would mean that every integer division should appear in a try block!
In fact, splitting can throw an ArithmeticException, which is an unchecked exception, so there is no need to catch it. In fact, you should not catch it, since this is an exceptional condition that has occurred and usually can only be resolved with the help of a code correction .
In your case, your code had to do something earlier in order to actually divide by zero.
If you have reached the step when you allow division by zero, then you cannot do anything. The program is erroneous, and it is better to fix it than to try to hide it in some way by throwing / catching an exception.

+1
source share

A short answer, as some of the pros and cons have been named: it is a matter of personal or organizational style. No functionally better than the other. You (or your project) will have to make your own decisions about whether to use checked or unchecked exceptions, or both.

The Oracle Java Tutorial advises you to use checked exceptions for all errors from which the application can be recovered, and unverified exceptions for errors that the application cannot recover from. But in my experience, most applications can recover from most (maybe not during startup) exceptions. The action will be interrupted, but the application will remain alive.

I would prefer to use only checked exceptions or excluded exceptions. Mixing can lead to confusion and inconsistent use. Be pragmatic. Do what makes sense in your situation.

0
source share

Never explicitly throw RuntimeException s. If you ever thought what you needed, then just allow the runtime, not using throw .

0
source share

All Articles