Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NumberFormatException not being caught in try/catch

I am having a problem with a .jsp page I am creating. For those worried, the site was for homework, however, I am trying to go beyond what is required and am not asking anything related to grading. This is strictly for my own benefit.

Down to business: I am getting an input from the user, performing a method="post" and refreshing the page, and in an ideal situation it works (that was the homework.) However, I am trying to make a try/catch block in the case where a user enters a string into an int field.

int Svolt = 0;
double Amperes = 0.0;
double LEDdrop = 0.0;

if (request.getParameter("Svolt") != null) {
try {
    Svolt = Integer.parseInt(request.getParameter("Svolt")); 
} catch ( NumberFormatException e ) {
    %>
    <script type ="text/javascript">
        alert("The source voltage must be an integer. Please fix this.");
    </script>
    <%                                                    
}

I've tried switching "NumberFormatException" to "Exception", but it returned the same error code. Earlier, I had my try catch come before the if() statement, and it worked, but didn't display the alert box I want.

That code is below:

final double MA_TO_A = 0.001;
int Svolt = 0;
double Amperes = 0.0;
double LEDdrop = 0.0;

try {
    if (request.getParameter("Svolt") != null) {
        try {
            Svolt = Integer.parseInt(request.getParameter("Svolt"));
        } catch (Exception e) 
        {
            %>
            <script type ="text/javascript">
                alert("The source voltage must be an integer. Please fix this.");
            </script>
            <%
        }
        Amperes = Double.parseDouble(request.getParameter("Amperes"));
        LEDdrop = Double.parseDouble(request.getParameter("LEDdrop"));

        out.println("Ideal resistance is: " + ((Svolt - LEDdrop) / (Amperes * MA_TO_A)) + " Ohms.");   
    }
} catch (Exception e) {
        out.println("Please make sure your inputs are correct.");
}

Any help is appreciated, and your time is, too. Thank you very much!

EDIT: Sorry, meant to copy-paste the error code:

org.apache.jasper.JasperException: java.lang.NumberFormatException: For input string: "a"

Where the tested input was the string "a"

EDIT2: Back-up/edit/test @Nambari's suggestion

final double MA_TO_A = 0.001;
int Svolt = 0;
double Amperes = 20.0;
double LEDdrop = 1.8;

try {
    if (request.getParameter("Svolt") != null) {
        try {
            Svolt = Integer.parseInt(request.getParameter("Svolt"));
        } catch (Exception e) 
        {
            %>
            <script type ="text/javascript">
                alert("The source voltage must be an integer. Please fix this.");
            </script>
        <%
        }
        out.println("Ideal resistance is: " + ((Svolt - LEDdrop) / (Amperes * MA_TO_A)) + " Ohms.");   
    }
} catch (Exception e) {
    out.println("Please make sure your inputs are correct.");
}

Output: Ideal resistance is: -90.0 Ohms

Alert box saying ("The source voltage must be an integer. Please fix this.")

No error code.

No out.println saying ("Please make sure your inputs correct").

FINAL EDIT:

Code now works almost as desired, but good enough.

                    final double MA_TO_A = 0.001;
                    int Svolt;
                    double Amperes;
                    double LEDdrop;

                    if (request.getParameter("Svolt") != null) 
                    {
                        try 
                        {

                            try 
                            {
                                Svolt = Integer.parseInt(request.getParameter("Svolt"));
                            } catch (Exception e) 
                            {
                            %>
                            <script type ="text/javascript">
                                alert("The source voltage must be an integer. Please fix this.");
                            </script>
                            <%  
                            Svolt = 0;
                            }
                            try 
                            {
                                Amperes = Double.parseDouble(request.getParameter("Amperes"));
                            } catch (Exception e) 
                            {
                                %>
                                <script  type = "text/javascript"> 
                                    alert("The source voltage must be an integer. Please fix this.");
                                </script> 
                                <% 
                                Amperes = 0.0;
                            }
                            try 
                            {
                                LEDdrop = Double.parseDouble(request.getParameter("LEDdrop"));
                            } catch (Exception e) 
                            {
                            %>
                                <script  type = "text/javascript"> 
                                    alert("The source voltage must be an integer. Please fix this.");
                                </script> 
                            <%
                            LEDdrop = 0.0;
                            }
                            out.println("Ideal resistance is: " + ((Svolt - LEDdrop) / (Amperes * MA_TO_A)) + " Ohms.");
                        } catch (Exception e)
                        { 
                            out.println("Please make sure your inputs are correct."); 
                        }   
                    }

Lots of jargon, I know, but that's the end result. Somehow, this has it all working. Thank you to all who helped!

like image 737
user1660454 Avatar asked Oct 06 '22 16:10

user1660454


2 Answers

First of all, it's not recommended to embed java code in a java servlet page, you should only take care of the presentation layer in the jsp, no business logic, you need a servlet to take the http request, do your logic, put the results on the response, and then redirect to the jsp.

On the other hand the exception type thrown by the JSP page is org.apache.jasper.JasperException, and not NumberFormatException, the apache jasper JSP engine is catching the number format exception, and throwing a JasperException to Tomcat.

Also, clear your tomcat cache to ensure that your JSP is being deployed to the last version, in order to do so, delete the following folders and files (including war):
{tomcat installation path}\work\Catalina\localhost(your app)
{tomcat installation path}\webapps(your app)

It should work if you catch the generic java.lang.Exception.

Also try this:


    final double MA_TO_A = 0.001;
    int Svolt = 0;
    double Amperes = 0.0;
    double LEDdrop = 0.0;
    try {
        if (request.getParameter("Svolt") != null) {
            try {
                Svolt = Integer.parseInt(request.getParameter("Svolt"));
             } catch (Exception e) 
            {
                //Changed to avoid the message if no exception, or for some other reason it fails to execute the conditions
                out.println("<script type =\"text/javascript\">")
                out.println("alert(\"The source voltage must be an integer. Please fix this.\");") ;
                out.println("</script>")
            }
            Amperes = Double.parseDouble(request.getParameter("Amperes"));
            LEDdrop = Double.parseDouble(request.getParameter("LEDdrop"));

            out.println("Ideal resistance is: " + ((Svolt - LEDdrop) / (Amperes * MA_TO_A)) + " Ohms.");   
        }
    } catch (Exception e) {
            out.println("Please make sure your inputs are correct.");
    }
like image 187
F. Mayoral Avatar answered Oct 10 '22 02:10

F. Mayoral


This is posted as answer based on my comments:

I would suggest remove everything except the Svolt related code on your form and try. I think something else messing up than this filed. Take a backup of existing code. Remove everything except Svolt and see, we can nail down the issue.

Make sure try/catch blocks are properly synched with code flow as suggested in above comments.

like image 33
kosa Avatar answered Oct 10 '22 02:10

kosa