Math.sqrt(-5) -> Force Exception in JSP?

空扰寡人 提交于 2019-12-24 09:51:07

问题


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>
<%}
%>

回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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 ;)




回答5:


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.




回答6:


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



来源:https://stackoverflow.com/questions/1813150/math-sqrt-5-force-exception-in-jsp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!