Java unchecked / exception clarification

I've been reading about unchecked and checked questions, and no online resource really knows these differences and when to use them

According to my understanding, they will be thrown at run time. They represent the program state beyond the logical expectation, but the checked exceptions must be explicitly caught, while the unchecked exceptions will not

My question is, suppose I have a way to divide two numbers for argument

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

And the method of using divison somewhere

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

Who is responsible for checking if the denominator is zero, and whether exceptions should be checked or unchecked (ignoring Java's built-in partition check)?

So, is the division method declared as is or as is

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 the exceptions without inspection are as follows:

double divide(double numerator,double denominator)
{
    if(denominator == 0) throw DivideByZeroException
    else ...
}

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

And allow foo to divide by zero check?

This problem first appeared in the mathematical program I wrote, in which users input numbers and logic classes to perform calculations I don't know whether the GUI should immediately check for incorrect values, or whether the internal logic should catch them and throw exceptions during calculation

Solution

Interesting topic indeed!

After reading and trying many ways to deal with general errors and exceptions, I learned the difference between programmer errors and expected errors

Programmer errors should never be caught, but crash early and difficult (!) Programmer errors are caused by logic errors and the root cause should be fixed

Expected errors should always be caught In addition, when an expected error is caught, a message must be displayed for the user This has an important meaning - if the expected error should not show an error, it is better to check whether the method will throw rather than let it throw

So when applied to your example, I would think, "how should this treat users?"

>If an error message should be displayed (in the browser output, console, message box), I will throw an exception and catch it as close to the UI as possible and output the error message. > If no error message is displayed, I will check the input instead of throwing it

Sidenote: I never throw dividebyzeroexception or NullPointerException - I let the JVM throw these things for me In this case, you can brew your own exception class or use the appropriate built-in exception checking

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>