NumberFormatException misses try / catch

I am having a problem with the .jsp page I created. For those who were worried, the site was for homework, however, I am trying to go beyond what is required, and I am not asking for anything related to the assessment. This is strictly for me.

Before business: I get input from the user by executing the method = "message" and refreshing the page, and in an ideal situation it works (it was homework). However, I am trying to make a try / catch block in the case where the user enters a string into the 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 tried to switch "NumberFormatException" to "Exception", but it returned the same error code. I used to have a try catch attempt before the if () statement, and it worked, but did not display the warning window that I want.

This 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.");
}

, . !

EDIT: , :

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

"a"

EDIT2: // @ Nambari

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.");
}

: : -90,0

( " . ".)

.

out.println, ( ", , " ).

:

, .

                    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."); 
                        }   
                    }

, , . , . , !

+5
2

, :

, Svolt, . , - , . . , Svolt, , .

, try/catch , .

+1

, java- Java, jsp, -, , HTTP-, , , jsp.

, , JSP, - org.apache.jasper.JasperException, NumberFormatException, JSP- apache jasper JasperException Tomcat.

, tomcat, , JSP , ( ):
{ tomcat}\work\Catalina\localhost ( )
{ tomcat}\webapps ( )

, java.lang.Exception.

:


    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.");
    }
+3

All Articles