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

时间:2012-10-12 14:08:36

标签: java

我有问题。编码如下所示。当我运行程序并输入“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

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

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

4 个答案:

答案 0 :(得分:1)

再添加一个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 {
        //....
    }

通常,您可以根据需要向单个catch块添加尽可能多的try块。但请记住给他们正确的顺序 - 首先放置更具体的例外,更通用 - 最后。

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

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

答案 1 :(得分:0)

添加更多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");

答案 2 :(得分:0)

由于您故意输入错误数据“aaa”,您的声明:

div=Integer.parseInt(inData);

将抛出NumberFormatException。您可以为此添加一个catch块:

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

答案 3 :(得分:0)

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

catch (ArithmeticException |ArrayIndexOutOfBoundsException ae)

我建议你抓住你期望的例外情况,而不仅仅是一条毯子的Throwable或Exception