【问题标题】:NumberFormatException not being caught in try/catchNumberFormatException 未在 try/catch 中被捕获
【发布时间】:2012-09-10 14:48:57
【问题描述】:

我正在创建的 .jsp 页面有问题。对于那些担心的人来说,该网站是用于家庭作业的,但是,我试图超越要求的范围,并且不询问与评分相关的任何事情。这完全是为了我自己的利益。

言归正传: 我从用户那里得到输入,执行 method="post" 并刷新页面,并且在理想情况下它可以工作(这是家庭作业。)但是,我正在尝试在这种情况下创建一个 try/catch 块用户在 int 字段中输入字符串。

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

我尝试将“NumberFormatException”切换为“Exception”,但它返回了相同的错误代码。 早些时候,我在 if() 语句之前有我的 try catch,它有效,但没有显示我想要的警告框。

代码如下:

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

感谢您的帮助,您的时间也是如此。非常感谢!

编辑: 抱歉,我想复制粘贴错误代码:

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

测试的输入是字符串“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.");
}

输出: 理想电阻为:-90.0 Ohms

警告框说(“源电压必须是整数。请解决这个问题。”)

没有错误代码。

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

我知道有很多行话,但这就是最终结果。不知何故,这一切正常。感谢所有帮助过的人!

【问题讨论】:

  • 错误码和异常信息是什么?顺便说一句,在 JSP 中嵌入 Java 代码并不是最佳实践。可能是 JSTL 等 EL API,会是不错的选择。
  • 您的问题表明它“返回了相同的错误代码”。它会产生什么错误?
  • 尝试将Amperes = Double.parseDouble(request.getParameter("Amperes")); LEDdrop = Double.parseDouble(request.getParameter("LEDdrop")); 放在一个较小的try 子句中。
  • 这就是不使用 scriptlet 的原因之一
  • 是否可以删除表单上除 Svolt 相关代码之外的所有内容并尝试?我认为除了这个文件之外还有其他事情搞砸了。备份现有代码。删除除 Svolt 之外的所有内容,看看,我们可以解决问题。

标签: java jsp try-catch


【解决方案1】:

首先,不建议在java servlet页面中嵌入java代码,你应该只关心jsp中的表示层,没有业务逻辑,你需要一个servlet来接受http请求,做你的逻辑,把结果放在response上,然后重定向到jsp。

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

另外,请清除您的 tomcat 缓存以确保您的 JSP 正在部署到最新版本,为此,请删除以下文件夹和文件(包括 war):
{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.");
    }

【讨论】:

    【解决方案2】:

    这是根据我的 cmets 作为答案发布的:

    我建议删除表单上除 Svolt 相关代码之外的所有内容并尝试。我认为除了这个文件之外还有其他事情搞砸了。备份现有代码。删除除 Svolt 之外的所有内容,看看,我们可以确定问题。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-04
      • 1970-01-01
      • 1970-01-01
      • 2020-02-28
      • 1970-01-01
      • 1970-01-01
      • 2011-05-15
      相关资源
      最近更新 更多