Java TRY CATCH 中的 IF 和 ELSE?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19010229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:20:22  来源:igfitidea点击:

IF and ELSE in TRY CATCH?

javaandroidxml

提问by Mariocci Rossini

I have this code

我有这个代码

root = new Root();
root.checkRootMethod2();
TextView=(TextView)view.findViewById(R.id.textView4); 

if(root.checkRootMethod2()) {
    TextView.setText(Html.fromHtml("<b>TEXT 01</b><br>"));
} else {
    TextView.setText(Html.fromHtml("<b>TEXT 02</b><br>"));
}

try {
    if (root.RootAvailibility() && (root.checkRootMethod3())) {
        try {
            Process process = Runtime.getRuntime().exec("su");
            OutputStream stdin = process.getOutputStream();
            stdin.flush();
            stdin.close();
        } catch(Exception e) {
        }
        TextView.append(Html.fromHtml(
            "<b><font color=\"green\">TEXT 03</b></font>"));
    }
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

try {
    root.busybox();
    TextView.append(Html.fromHtml(
        "<br><b><font color=\"green\">TEXT 04</b></font>"));
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch(Exception e) {
    TextView.append(Html.fromHtml(
        "<br><b><font color=\"red\">TEXT05</b></font>"));
}

I wish that if if (root.RootAvailibility() && (root.checkRootMethod3())) return trueViewing a TextViewthat says something.If return false, another TextView that displays something else. As happens for root.checkRootMethod2 ();Same goes for root.busybox ();Do you have any idea on how I can do? Now visualize always Text04

我希望如果 if (root.RootAvailibility() && (root.checkRootMethod3())) return trueViewing a TextViewthat say something.If 返回 false,另一个显示其他内容的 TextView。就像发生的事情root.checkRootMethod2 ();一样,root.busybox ();你知道我该怎么做吗?现在总是形象化Text04

回答by Gaurav Varma

I don't know what is the need of multiple try/catch here :

我不知道这里需要多次尝试/捕获:

try {
    if (root.RootAvailibility() && (root.checkRootMethod3()))
    {

    try
    {

You can add one more catch(Exception e) to the upper try/catch block and that will serve the same purpose. Secondly there is no else part to this if (root.RootAvailibility() && (root.checkRootMethod3())). So, if it is false the program will simply move forward.

您可以在上面的 try/catch 块中再添加一个 catch(Exception e),这将达到相同的目的。其次,这个 if 没有其他部分(root.RootAvailibility() && (root.checkRootMethod3()))。所以,如果它是假的,程序将简单地向前推进。

回答by crownjewel82

Well you're always going to see Text04 because there's no conditional that excludes it. The try catch block it's in is at the top level.

好吧,您总是会看到 Text04,因为没有排除它的条件。它所在的 try catch 块位于顶层。

It would help if you could provide a short, self-contained, compilable example of your code. There's clearly other potentially relevant code missing. For example, the try that goes with that last catch block. Also, it might help you to comment the beginning and end of your code blocks so that you can tell what's included in the if else statements.

如果您能提供一个简短的、自包含的、可编译的代码示例,将会有所帮助。显然缺少其他可能相关的代码。例如,与最后一个 catch 块一起使用的 try。此外,它可能会帮助您注释代码块的开头和结尾,以便您可以了解 if else 语句中包含的内容。

回答by Bijaya Bidari

try {
    if (root.RootAvailibility() && (root.checkRootMethod3()))
    {
       try
       {
        /// your code ...
        } 
        catch(Exception e){ }

     TextView.append(Html.fromHtml("<b><font color=\"green\">TEXT 03</b></font>"));
    }
  }catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }
// Codes here runs always regardless of if clause. 

the code (tryblock in your case) runs regardless of the ifcondition as the tryblock clears the scope of ifblock.
Either put trycompletely inside ifblock or surround both if,elsestatement by a single tryblock.

try无论if条件如何,代码(在您的情况下都是块)都会运行,因为try块会清除块的范围if
要么try完全放在if块内,要么if,else用一个try块包围这两个语句。