我有一个小问题,如果我们可以处理if else块,那么DivisionByZeroException
的用法是什么。我试着用谷歌搜索,但不能得到正确的答案。任何人都可以详细地告诉我吗?提前致谢DivisionByZero通过使用if else语句
回答
忘掉DivisionByZeroException,几乎所有的异常都可以通过使用if-else逻辑来避免。
例外情况的要点是从某种意外情况中恢复并简化此恢复。如果有10个地方可能会在您的代码中出现异常,则必须确保您已包含所有if-else条件。异常处理简化了这一点。你不必在每一个可能的地方进行验证,只要尝试一下,就会发现一旦发生异常情况。
这也提供了简单的方法来为不同的例外提供不同的恢复机制。
if(check for first type of exception)
{
do first task
}
else
{
return one type of error
}
do some intermediary task
if(check for first type of exception && check for second type of exception)
{
do second task
}
else
{
if(exception is of one type)
return one type of error
if(exception is of second type)
return another type of error
}
上面的代码可以更清楚,如果你使用一些尝试catch块如下...
try{
do first task
do some intermediary task
do second task
}
catch(first type of exception)
{
return one type of error
}
catch(second type of exception)
{
return second type of error
}
catch(another type of exception developer may have forgotten)
{
return a generic error
}
第二种方法显然会更加清楚,一旦你获得了异常处理的一些好的知识就像其他人一样。在第二种方法中,代码流更容易明显。
另外还有一件东西是佛陀说的。假设你正在使用第三方api(JXL/JNA等)..你不知道如果你传递错误的参数值会发生什么..被调用的函数可能会说“throws someException”(错误的编程实践顺便说一句)...在这种情况下,你不知道你会得到什么异常..如果你不知道你会得到什么异常,你如何使用if-else来防止它? ..使用异常,你可以通过一般化的方式来捕捉它,并将其作为“JXLException/JNAException ..”来处理。 – TheLostMind
是的...好点。你并不总是知道除了什么。 – Buddha
- 1. if else if else语句(JS)
- 2. Android:参数不通过if-else语句?
- 3. 使用if和else语句
- 4. PHP:使用IF ELSE语句
- 5. 使用IF/ELSE语句
- 6. 使用if else语句
- 7. java跳过if else语句
- 8. if/else语句
- 9. If else语句
- 10. if else语句
- 11. if-else语句
- 12. If/else语句
- 13. 代码搜索栏通过使用if else语句
- 14. 通过Javascript设置CSS属性if/else语句使用Jquery
- 15. JavaScript - 使用for循环和if-else语句来通过数组
- 16. jquery if else语句
- 17. Big if else语句
- 18. IF ELSE语句SQLite
- 19. Python if else语句
- 20. jQuery if/else语句
- 21. PHP if/else语句
- 22. “形” - if-else语句
- 23. 的if else语句
- 24. PHP IF ELSE语句
- 25. PHP - if ... else ...语句
- 26. VHDL If/Else语句
- 27. jquery if else语句
- 28. string.format if else语句
- 29. hash.indexOf()if else语句
- 30. Oracle IF/ELSE语句
不是一个标准问题 –
有人会问这个问题吗? –
在java中它是[ArithmeticException](http://docs.oracle.com/javase/7/docs/api/java/lang/ArithmeticException.html) –