I've 3 .jsp files. index.jsp and sqrtcalculator.jsp and error.jsp.
In index.jsp there's a text field and a button. In sqrtcalculator.jps there's the, well, calculator.
Now, when I leave the field empty and press the button, an expection is called, because the field is empty. And I can then reroute to an individual error.jsp site to display the errors, and exception stack like
<%= request.getAttribute("javax.servlet.error.request_uri") %>
<%= exception.getMessage() %>
<%= exception %>
etc.
Problem is, when I enter -5 in the textfield, I can't get an exception, because Math.开发者_高级运维sqrt(-5) doesn't return an error but "NaN".
How can I force it to be an exception? The idea is to reroute the user to an individual error.jsp and displaying the errors. But like I said Math.sqrt(-5) doesn't return errors or exceptions but "NaN" textstring.
I don't want something like this just FYI
<%if (NaN){
%>
<p>Nope, Square root from negative numbers is not defined!</p>
<%}
%>
Warning, the test for NaN in the previous answer is wrong (NaN is NOT equal to itself). In Java the better way to test for NaN is
if (Double.isNaN(answer)) {
// throw exception
}
alternatively
if (answer != answer) {
}
While this second version will work, it is sure to puzzle those not aware of the curious behaviour of NaN.
How about:
if (x >= 0.0) {
return Math.sqrt(x);
} else {
throw new ArithmeticException("Square root from negative numbers is not defined!");
}
Note that ArithmeticException
is a subclass of RuntimeException
and therefore does not need to be declared in the throws
clause of your function.
It's presumably returning NaN
because square roots of negative numbers are indeed defined, they're just not real numbers. Is there any reason you can't do this?
if(Double.isNaN(answer))
throw new ArithmeticException("Answer unreal");
That should tickle your exception handling code, but the source line might just be a few lines off. I've never written JSP, but that makes sense in Java.
I'm not very good in jsp but afaik you should be able to use basic java code.. so i'd check my result for NaN and throw an exception
if (NaN == result)
throw new Exception("NaN");
you'll have to adjust the if ;)
I would ask why you're using scriptlets in JSPs.
If your app really is just three pages (index.jsp and sqrtcalculator.jsp and error.jsp), then maybe it's justifiable.
But my general recommendation would be to stay away from scriptlet code in JSPs and prefer JSTL. Have those calculations done in a server-side component, using a Model-2 MVC arrangement.
If your app really consists of only three pages, re-architecting it to use Model-2 MVC would not be difficult to do. You might find that it's far more extensible that way. If you're unfamiliar with Model-2 MVC you can read about it here. If you're familiar with it, this might be a self-contained problem that's small enough to let you see where it could provide some value in future projects.
Another thought would be to add complex numbers to your calculator, because the square root of negative numbers is indeed a valid concept. It just requires imaginary numbers. Maybe your calculator needs to be extended to maximize usefulness.
I can't seem to get it to work....
if (d < 0){
throw new Exception("Negative Numbers not possible!");
}
d is a double read via d_string = request.getParameter("numberfromtextfield") and converted to double via double d = Double.parseDouble(d_string);.
This is what Eclipse is telling me: http://pastebin.ca/1691409
精彩评论