除以零错误或异常? [英] Is divide by zero an error or an exception?
问题描述
基本上我想知道你如何区分错误和异常。在一些编程语言中,访问不存在的文件会引发错误,而在其他编程语言中会引发异常。你如何知道某些事情是错误还是例外?
和其他任何事情一样 - 你要么测试,要么读文档。它可以是基于该语言的错误或异常。
例如
C:
崩溃并给出零错误。
Ruby:
>> 6/0
pre>
ZeroDivisionError:从(irb)除以0
:1:在`/'
from(irb):1
(ZeroDivisionError实际上是一个例外。)
Java: p>
代码:
int x = 6/0;
输出:
code>线程main中的异常java.lang.ArithmeticException:/ by zero
Basically I want to know how do you differentiate an error from an exception. In some programming languages accessing a non existent file throws an error and in others its an exception. How do you know if some thing is an error or an exception?
解决方案Like anything else - you either test it or read the documentation. It can be an "Error" or an "Exception" based on the language.
Eg.
C:
Crashes and gives a divide by zero error.
Ruby:
>> 6 / 0 ZeroDivisionError: divided by 0 from (irb):1:in `/' from (irb):1
(ZeroDivisionError is actually an exception.)
Java:
Code:
int x = 6 / 0;
Output:
Exception in thread "main" java.lang.ArithmeticException: / by zero
这篇关于除以零错误或异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!