java 程序捕获算术异常并将演示 finally 块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12860866/
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-10-31 10:31:46  来源:igfitidea点击:

program catch the Arithmetic Exception and will demonstrate the finally block

java

提问by Rebecca

I have problem. The coding is shown as below. When I run the program and enter "aaa", it shows error because it only catch arithmetic exception. How to add an appropriate codes to overcome the exception based on the problem occur?

我有问题。编码如下所示。当我运行程序并输入“aaa”时,它显示错误,因为它只捕获算术异常。如何根据出现的问题添加适当的代码来克服异常?

import java.io.* ;
public class FinallyPractice1 
{
    public static void main(String [])
    {
        BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
        String inData; int num=0, div=0;
        try
        {   System.out.println("Enter the numerator:");
            inData=stdin.readLine();
            num=Integer.parseInt(inData);

            System.out.println("Enter the divisor:");
            inData=stdin.readLine();
            div=Integer.parseInt(inData);

            System.out.println(num+"/"+div+"  is  "+(num/div));
        }
        catch(ArrayIndexOutOfBoundsException ae)
        {
        System.out.println("You can't divide "+ num + " by " + div);
        }
         catch(ArithmeticException aex)
         {
          System.out.println("You entered not a number: " + inData);
        }
        finally
        {
        System.out.println("If the division didn't work, you entered bad data.");
        }
            System.out.println("Good-by");
    }
}

enter image description here

在此处输入图片说明

I already find the answers! The coding is like below:

我已经找到答案了!编码如下:

 import java.io.* ;
    public class FinallyPractice1 
    {
        public static void main(String [] a) throws IOException
        {
            BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
            String inData; int num=0, div=0;
            try
            {   System.out.println("Enter the numerator:");
                inData=stdin.readLine();
                div=Integer.parseInt(inData);

                System.out.println("Enter the divisor:");
                inData=stdin.readLine();
                div=Integer.parseInt(inData);

                System.out.println(num+"/"+div+"  is  "+(num/div));
            }
            catch(ArithmeticException ae)
                  {
                      System.out.println("ArithmeticException by " + div);
                  }
                    catch(ArrayIndexOutOfBoundsException ae)
                    {
                    System.out.println("You can't divide "+ num + " by " + div);
                 }
                    catch(NumberFormatException ae)
                    {
                  System.out.println("NumberException");
                 }
                    finally
                  {
                    System.out.println("If the division didn't work, you entered bad data.");
                 }
                      System.out.println("Good-by");
        }
    }

回答by HitOdessit

Add one more catchblock, like this:

再添加一个catch块,如下所示:

    } catch(ArrayIndexOutOfBoundsException ae) {
        System.out.println("You can't divide "+ num + " by " + div);
    } catch(ArithmeticException aex) {
        System.out.println("You entered not a number: " + inData);
    } finally {
        //....
    }

Generaly, you can add as many catchblocks to a single tryblock as you want. But remember to give them correct order - place more specific exceptions first, and more generic - last.

通常,您可以根据需要catch将任意数量的try块添加到单个块中。但是请记住给它们正确的顺序——首先放置更具体的例外,然后放置更通用的例外——最后。

If you want to catch any possible exception, you can use Throwableclass:

如果要捕获任何可能的异常,可以使用Throwable类:

try {
    // some potentially dangerous code
} catch (Throwable th) {
    // process error somehow
}

回答by amicngh

add few more catch blocksto handle exceptions.

添加更多catch blocks来处理异常。

  catch(ArithmeticException ae)
        {
            System.out.println("ArithmeticException by " + div);
        }
        catch(ArrayIndexOutOfBoundsException ae)
        {
        System.out.println("You can't divide "+ num + " by " + div);
        }
        catch(IOException ae)
        {
        System.out.println("IOException");
        }
        finally
        {
        System.out.println("If the division didn't work, you entered bad data.");
        }
            System.out.println("Good-by");

回答by Reimeus

As you are deliberately inputting bad data "aaa", your statement:

当您故意输入错误数据“aaa”时,您的陈述:

div=Integer.parseInt(inData);

will throw a NumberFormatException. You could add a catch block for this:

会抛出一个NumberFormatException. 您可以为此添加一个 catch 块:

...
} catch (NumberFormatException nfe) {
   System.err.println(nfe);
  // more error handling
} catch ...

回答by RNJ

You can use the new multi-catch. It has the from

您可以使用新的multi-catch。它有来自

catch (ArithmeticException |ArrayIndexOutOfBoundsException ae)

I would recommend you catch the exceptions you are expecting rather than just a blanket Throwable or Exception

我建议您捕获您期望的异常,而不仅仅是一揽子 Throwable 或 Exception