Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is divide by zero an error or an exception?

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 image 731
Kumar Avatar asked Dec 18 '25 14:12

Kumar


2 Answers

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
like image 119
AndrewKS Avatar answered Dec 21 '25 02:12

AndrewKS


It depends on the language :

  • some languages don't have exceptions
  • some languages don't use exceptions for everything.


For example, in PHP :

  • There are exceptions
  • But divide by 0 doesn't cause an exception to be thrown : is only raises a warning -- that doesn't stop the execution of the script.

The following portion of code :

echo 10 / 0;
echo "hello, world!";

Would give this result :

Warning: Division by zero in /.../temp.php on line 5
hello, world!
like image 28
Pascal MARTIN Avatar answered Dec 21 '25 04:12

Pascal MARTIN



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!