Java未经检查/检查异常澄清 [英] Java unchecked/checked exception clarification

查看:129
本文介绍了Java未经检查/检查异常澄清的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读有关未经检查的和已检查的问题,没有一个在线资源已经清楚了解差异以及何时使用。



从什么我了解,他们都在运行时抛出,它们都代表超出逻辑预期范围的程序状态,但检查的异常必须明确地被捕获,而未选中的异常则不会。


$ b $我的问题是,假设为了论证,我有一个方法来分割两个数字

  double divide(double fractator,双分母)
{返回分子/分母;

和需要divison的方法

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

谁负责检查分母为零的情况,应检查异常或没有选择(忽略Java内置的divison检查)?



所以,分隔方法是否被声明为或者/ b
$ b

  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)
{}
}

或没有被检查的异常,就像这样:

  double divide(双分子,双分母)
{
if(denominator == 0)throw DivideByZeroException
else ...
}

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

并允许foo将零除以



这个问题最初出现在我写的数学程序中,用户输入的数字和逻辑类进行了计算。我不能确定GUI是否立即检查不正确的值,或内部逻辑是否应在计算过程中捕获它们并抛出异常。

解决方案

有趣的话题确实!



在阅读和尝试很多方法来处理一般的错误和异常之后,我学会了不同的程序员错误和预期错误



程序员错误永远不会被抓住,而是早期和困难地崩溃(!)程序员错误是由于逻辑故障引起的,根本原因应该是固定的。



应该总是被捕获。此外,当捕获预期的错误时,必须为用户显示消息。这有一个重要的含义 - 如果预期的错误不应该显示错误,最好检查方法是否会抛出,而不是让它抛出。



所以应用于你的例如,我会认为应该如何看待用户?


  1. 如果显示错误消息(在浏览器输出中,控制台,messagebox)我会抛出一个异常并尽可能接近UI 并输出错误消息。

  2. 如果没有错误信息显示我会检查输入,而不是抛出。

在sidenote上:我从不抛出 DivideByZeroException NullPointerException - 我让JVM为我抛出。在这种情况下,您可以创建自己的异常类或使用适当的内置检查异常。


I've been reading about unchecked versus checked questions, none of the online resources have been truly clear about the difference and when to use both.

From what I understand, both of them get thrown at runtime, both of them represent program states that are outside the expected bounds of the logic, but checked exceptions must be explicitly caught while unchecked ones do not.

My question is, suppose for argument's sake I have a method that divides two numbers

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

and a method that requires divison somewhere

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

Who is responsible for checking the case of the denominator being zero, and should an exception be checked or unchecked (ignoring Java's built in divison checks)?

So, would the divide method be declared as is or as

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 allow foo to make the divide by zero check?

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

解决方案

Interesting topic indeed!

After reading and trying lots of way to deal with errors in general and exceptions specifically I learned to differ between programmer errors and a expected errors.

Programmer errors should never be caught, but rather crash (!) early and hard. A programmer error is due to a logical fault, and the root cause should be fixed.

Expected errors should always be caught. Also when a expected error is caught a message must be displayed for the user. This has an important implication - If a expected error should not display an error, it's better to check whether the method will throw instead of letting it throw.

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

  1. If a error-message should be displayed (in the browser output, console, messagebox) I would throw an exception and catch it as close to the UI as possible and output the error-message.
  2. If no error message should be displayed I would check the input and not throw.

On a sidenote: I never throw DivideByZeroException nor NullPointerException - I let the JVM throw those for me. In this case you could brew your own exception-class or use a suitable built-in checked exception.

这篇关于Java未经检查/检查异常澄清的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆