Just noticed that my school book says that "Math.sqrt(-9) gives a Runtime Error" . Now, I am confused since when I tried this on IntelliJ and BlueJ IDE, none of them gave me a RuntimeException
, instead they both returned NaN
. Furthermore, the book says that "/ by 0" is a "Runtime error" since it leads to an ArithmeticException
. Now I am confused between what is categorised as an Error and what is categorised as an Exception.
Edit:Please do not confuse this with the Error which extends the Throwable class in java.I am assuming the book means error as an English word and not the aforementioned Error
.
CodePudding user response:
I Googled for that sentence, and it appears in a sample answer to what appears to be a sample exam question.
(d) Name the type of error (syntax, runtime or logical error) in each case given below:
(i) Math.sqrt (36-45)
(ii) int a;b;c;
This question shows up in a lot of different search results. The funny thing is that while they all agree that (ii)
is a syntax error, they disagree on the answer to (i)
. Some say it is a runtime error, others a logic error1.
The real answer is the correct answer to (i)
depends on the context.
- In some programming languages, this may result in a runtime exception.
- In other programming languages, it will produce the value
NaN
. - In a programming language that natively supported complex numbers, the answer would be
3i
.
To determine whether this is an error at all, you first need to know which programming language we are talking about. It looks like Java2, but there are other possibilities. Then you need to decide whether the program is expecting the predicted exception or NaN
or whatever. Consider this Java example:
try {
int a = 1;
int b = 0;
int c = a / b; // This will throw an exception in Java.
} catch (AritheticException ex) {
System.out.println("Success!");
}
The division by zero in the above is (arguably) not an error at all. We were expecting it, and we handled it. An exception in Java is a way to signal an exceptional event. It is not necessarily a runtime error.
Back to the original sqrt(-9)
example, in that case Java does not throw an exception. Instead it returns NaN
which is a legitimate IEE 768 floating point value. Is that an error? Well it actually depends on that the program does with that value.
In short: the question is ambiguous, and doesn't have a single correct answer.
It is a shame that school curricula are perpetuating this kind of thinking. One would hope that both "runtime error" and "logical error" would be accepted as correct answers if this particular question came up in a real exam. But ...
1 - One answer even said it was a syntax error because there is no semicolon.
2 - Even assuming this is Java, Math.sqrt
is not necessarily the java.lang.Math.sqrt(double)
method. It could be a different Math
class, or if the programmer is ignoring style, Math
could even be a variable name.
CodePudding user response:
“When I use a word,’ Humpty Dumpty said in rather a scornful tone, ‘it means just what I choose it to mean — neither more nor less.’ (Lewis Carroll)
There are neither general nor precise nor objective answers to your question. What people mean by the words they say or write — I guess the best you can do is ask those people.
So here’s my personal understanding.
RuntimeException
has a precise meaning since it is a Java class. A RuntimeException
is any exception of this class or one of its many subclasses including ArithmeticException
.
To me a runtime error is any error happening on runtime. Usually implying that the error was not or could not be detected at compile time. What constitutes an error is not well-defined and sometimes subjective. If the program crashes, it’s usually considered an error. If it gives incorrect output, too. Is it an error if the program fails to transmit some data in the first attempt but succeeds in the second? Is it an error that the user types incorrect data and is notified of it?
All exceptions thrown are thrown on runtime. This is noting special for RuntimeException
. The name RuntimeException
originally meant an exception coming from the Java Runtime Engine (JRE) rather than being thrown in Java code. Today very many RunetimeException
s are thrown using the throw
statement in Java.
To me an exception being thrown is not necessarily an error. Sometimes we must use exceptions for validation. I tend to think that an exceptions is an error if the programmer had not foreseen the possibility of it and does not handle it gracefully.
"Math.sqrt(-9) gives a Runtime Error"
This may have been written by a person who was not aware that NaN
is returned but expected an ArithmeticException
or other exception.