Java 在try/catch中未捕获NumberFormatException

Java 在try/catch中未捕获NumberFormatException,java,jsp,try-catch,Java,Jsp,Try Catch,我创建的.jsp页面有问题。对于那些担心的人来说,这个网站是用来做作业的,然而,我正在努力超越要求,并且没有要求任何与评分相关的东西。这完全是为了我自己的利益 具体到业务: 我从用户那里得到一个输入,执行method=“post”并刷新页面,在理想的情况下它可以工作(这是家庭作业)。但是,如果用户在int字段中输入字符串,我尝试创建一个try/catch块 int Svolt = 0; double Amperes = 0.0; double LEDdrop = 0.0; if (reques

我创建的.jsp页面有问题。对于那些担心的人来说,这个网站是用来做作业的,然而,我正在努力超越要求,并且没有要求任何与评分相关的东西。这完全是为了我自己的利益

具体到业务: 我从用户那里得到一个输入,执行method=“post”并刷新页面,在理想的情况下它可以工作(这是家庭作业)。但是,如果用户在int字段中输入字符串,我尝试创建一个try/catch块

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>
    <%                                                    
}
其中,测试输入是字符串“a”

编辑2: 备份/编辑/测试@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.");
}
最终双MA至A=0.001;
int Svolt=0;
双安培=20.0;
双LED下降=1.8;
试一试{
if(request.getParameter(“Svolt”)!=null){
试一试{
Svolt=Integer.parseInt(request.getParameter(“Svolt”);
}捕获(例外e)
{
%>
警报(“电源电压必须是整数。请修复此问题。”);
警报(“电源电压必须是整数。请修复此问题。”);
警报(“电源电压必须是整数。请修复此问题。”);
警报(“电源电压必须是整数。请修复此问题。”);

这是根据我的评论发布的答案:

我建议删除表单上除Svolt相关代码之外的所有内容,然后再试一次。我认为除了此字段之外,还有其他一些问题。请备份现有代码。删除除Svolt之外的所有内容,然后看看,我们可以解决这个问题


确保try/catch块与上述注释中建议的代码流正确同步。

首先,不建议在java servlet页面中嵌入java代码,您应该只考虑jsp中的表示层,而不考虑业务逻辑,您需要一个servlet来处理http请求,执行逻辑,将结果放在r上然后重定向到jsp

另一方面,JSP页面引发的异常类型是org.apache.jasper.jaspereException,而不是NumberFormatException,apache jasper JSP引擎正在捕获数字格式异常,并向Tomcat引发JaspereException

另外,清除tomcat缓存以确保JSP部署到最新版本,为此,请删除以下文件夹和文件(包括war):
{tomcat安装路径}\work\Catalina\localhost(您的应用程序)
{tomcat安装路径}\webapps(您的应用程序)

如果捕获了泛型java.lang.Exception,它应该可以工作

还可以尝试以下方法:


最终双MA至A=0.001;
int Svolt=0;
双安培=0.0;
双LED下降=0.0;
试一试{
if(request.getParameter(“Svolt”)!=null){
试一试{
Svolt=Integer.parseInt(request.getParameter(“Svolt”);
}捕获(例外e)
{
//如果没有异常或由于其他原因无法执行条件,则更改以避免消息
out.println(“”)
out.println(“警报(\”源电压必须是整数。请解决此问题。\”;);
out.println(“”)
}
安培=Double.parseDouble(request.getParameter(“安培”);
LEDdrop=Double.parseDouble(request.getParameter(“LEDdrop”));
println(“理想电阻为:”+((Svolt-LED压降)/(安培*毫安到毫安))+“欧姆”);
}
}捕获(例外e){
println(“请确保您的输入正确。”);
}

错误代码和异常消息是什么?顺便说一句,在JSP中嵌入Java代码不是最佳做法。可能像JSTL等EL API是不错的选择。您的问题指出它“返回了相同的错误代码”。它会产生什么错误?试着输入
ampers=Double.parseDouble(request.getParameter(“ampers”));LEDdrop=Double.parseDouble(request.getParameter(“LEDdrop”))
在一个较小的
try
子句中。这是不使用脚本的一个原因。是否可以删除表单上除了与Svolt相关的代码之外的所有内容并尝试?我认为还有一些比此字段更混乱的内容。对现有代码进行备份。删除除Svolt之外的所有内容,然后看,我们可以解决这个问题。
org.apache.jasper.JasperException: java.lang.NumberFormatException: For input string: "a"
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.");
}
                    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."); 
                        }   
                    }
    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.");
    }