<legend id='hIxGj'><style id='hIxGj'><dir id='hIxGj'><q id='hIxGj'></q></dir></style></legend>
    <bdo id='hIxGj'></bdo><ul id='hIxGj'></ul>
  • <small id='hIxGj'></small><noframes id='hIxGj'>

      1. <i id='hIxGj'><tr id='hIxGj'><dt id='hIxGj'><q id='hIxGj'><span id='hIxGj'><b id='hIxGj'><form id='hIxGj'><ins id='hIxGj'></ins><ul id='hIxGj'></ul><sub id='hIxGj'></sub></form><legend id='hIxGj'></legend><bdo id='hIxGj'><pre id='hIxGj'><center id='hIxGj'></center></pre></bdo></b><th id='hIxGj'></th></span></q></dt></tr></i><div id='hIxGj'><tfoot id='hIxGj'></tfoot><dl id='hIxGj'><fieldset id='hIxGj'></fieldset></dl></div>

      2. <tfoot id='hIxGj'></tfoot>

        Java中的String to Int - 可能是坏数据,需要避免异常

        String to Int in java - Likely bad data, need to avoid exceptions(Java中的String to Int - 可能是坏数据,需要避免异常)
          <tbody id='ainmM'></tbody>

        1. <i id='ainmM'><tr id='ainmM'><dt id='ainmM'><q id='ainmM'><span id='ainmM'><b id='ainmM'><form id='ainmM'><ins id='ainmM'></ins><ul id='ainmM'></ul><sub id='ainmM'></sub></form><legend id='ainmM'></legend><bdo id='ainmM'><pre id='ainmM'><center id='ainmM'></center></pre></bdo></b><th id='ainmM'></th></span></q></dt></tr></i><div id='ainmM'><tfoot id='ainmM'></tfoot><dl id='ainmM'><fieldset id='ainmM'></fieldset></dl></div>
          <legend id='ainmM'><style id='ainmM'><dir id='ainmM'><q id='ainmM'></q></dir></style></legend>

            <small id='ainmM'></small><noframes id='ainmM'>

                <bdo id='ainmM'></bdo><ul id='ainmM'></ul>
              • <tfoot id='ainmM'></tfoot>

                  本文介绍了Java中的String to Int - 可能是坏数据,需要避免异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  Java 没有可空类型,也没有 TryParse(),如何在不抛出异常的情况下处理输入验证?

                  Seeing as Java doesn't have nullable types, nor does it have a TryParse(), how do you handle input validation without throwing an exceptions?

                  通常的方式:

                  String userdata = /*value from gui*/
                  int val;
                  try
                  {
                     val = Integer.parseInt(userdata);
                  }
                  catch (NumberFormatException nfe)
                  {
                     // bad data - set to sentinel
                     val = Integer.MIN_VALUE;
                  }
                  

                  我可以使用正则表达式来检查它是否可解析,但这似乎也有很多开销.

                  I could use a regex to check if it's parseable, but that seems like a lot of overhead as well.

                  处理这种情况的最佳做法是什么?

                  What's the best practice for handling this situation?

                  理由:关于异常处理的 SO 讨论很多,普遍的态度是异常应该只用于意外情况.但是,我认为糟糕的用户输入是意料之中的,并不罕见.是的,这确实是一个学术观点.

                  Rationale: There's been a lot of talk on SO about exception handling, and the general attitude is that exceptions should be used for unexpected scenarios only. However, I think bad user input is EXPECTED, not rare. Yes, it really is an academic point.

                  进一步

                  一些答案确切地说明了 SO 有什么问题.你忽略了被问到的问题,并回答了另一个与它无关的问题.问题不是询问层之间的过渡.如果数字不可解析,问题不是要返回什么.众所周知,val = Integer.MIN_VALUE;对于这个完全上下文无关的代码片段所来自的应用程序来说,这完全是正确的选择.

                  Some of the answers demonstrate exactly what is wrong with SO. You ignore the question being asked, and answer another question that has nothing to do with it. The question isn't asking about transition between layers. The question isn't asking what to return if the number is un-parseable. For all you know, val = Integer.MIN_VALUE; is exactly the right option for the application that this completely context free code snippet was take from.

                  推荐答案

                  差不多就是这样,虽然返回 MIN_VALUE 有点问题,除非你确定它是用于你本质上用作错误代码.不过,至少我会记录错误代码行为.

                  That's pretty much it, although returning MIN_VALUE is kind of questionable, unless you're sure it's the right thing to use for what you're essentially using as an error code. At the very least I'd document the error code behavior, though.

                  也可能有用(取决于应用程序)记录错误输入以便您进行跟踪.

                  Might also be useful (depending on the application) to log the bad input so you can trace.

                  这篇关于Java中的String to Int - 可能是坏数据,需要避免异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Compiling C++ for the JVM(为 JVM 编译 C++)
                  Compile to java bytecode (without using Java)(编译成java字节码(不使用Java))
                  How to drive C#, C++ or Java compiler to compute 1+2+3+...+1000 at compile time?(如何在编译时驱动 C#、C++ 或 Java 编译器计算 1+2+3+...+1000?)
                  Java ClassLoader: load same class twice(Java ClassLoader:两次加载相同的类)
                  How to debug .class files in ECLIPSE?(如何在 ECLIPSE 中调试 .class 文件?)
                  Java quot;The blank final field may not have been initializedquot; Anonymous Interface vs Lambda Expression(Java“可能尚未初始化空白的最终字段匿名接口与 Lambda 表达式)
                    <tfoot id='kDCWV'></tfoot>
                        <bdo id='kDCWV'></bdo><ul id='kDCWV'></ul>
                          <tbody id='kDCWV'></tbody>

                          1. <i id='kDCWV'><tr id='kDCWV'><dt id='kDCWV'><q id='kDCWV'><span id='kDCWV'><b id='kDCWV'><form id='kDCWV'><ins id='kDCWV'></ins><ul id='kDCWV'></ul><sub id='kDCWV'></sub></form><legend id='kDCWV'></legend><bdo id='kDCWV'><pre id='kDCWV'><center id='kDCWV'></center></pre></bdo></b><th id='kDCWV'></th></span></q></dt></tr></i><div id='kDCWV'><tfoot id='kDCWV'></tfoot><dl id='kDCWV'><fieldset id='kDCWV'></fieldset></dl></div>

                            <small id='kDCWV'></small><noframes id='kDCWV'>

                          2. <legend id='kDCWV'><style id='kDCWV'><dir id='kDCWV'><q id='kDCWV'></q></dir></style></legend>