java 批量处理 示例_Java中异常处理的示例

java 批量处理 示例

Here, we will analyse some exception handling codes, to better understand the concepts.

在这里,我们将分析一些异常处理代码 ,以更好地理解这些概念。

Try to find the errors in the following code, if any

尝试在以下代码中查找错误(如果有)

Code 1:

代码1:

public class prog {
    public static void main(String arg[]) {
        try {
            int a = 10, b = 0;
            int c = a / b;
        } catch (RuntimeException e) {
            System.out.println(e.getMessage());
        } catch (ArithmeticException e) {
            System.out.println(e.getMessage());
        }
    }

}

Output

输出量

/prog.java:8: error: exception ArithmeticException has already been caught
        } catch (ArithmeticException e) {
          ^
1 error

Explanation:

说明:

When using multiple catch blocks, we have to make sure that the exceptions are caught in the reverse order of inheritance of the exceptions. This means that most specific exceptions must be caught before the most general exceptions. ArithmeticException must be caught before the RuntimeException.

当使用多个catch块时,我们必须确保以与异常继承相反的顺序捕获异常。 这意味着必须在最一般的异常之前捕获最具体的异常。 必须在RuntimeException之前捕获ArithmeticException



Code 2:

代码2:

public class prog {
    public static void main(String arg[]) {
        try {
            throw new Integer(25);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

Output

输出量

/prog.java:4: error: incompatible types: Integer cannot be converted to Throwable
            throw new Integer(25);
            ^
Note: /prog.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
1 error

Explanation:

说明:

We can only throw objects of Throwable class or classes that inherit this class. Integer class does not extend the Throwable class and thus, we cannot throw objects of Integer class. Similarly the statement "throw 25" is erroneous. Unlike C++, where we can throw objects, in Java we can only throw those objects that inherit the Throwable class.

我们只能抛出Throwable类或继承该类的类的对象。 Integer类不会扩展Throwable类,因此,我们不能抛出Integer类的对象。 类似地, “ throw 25”的陈述是错误的 。 与C ++可以抛出对象不同,在Java中,我们只能抛出那些继承Throwable类的对象。



Code 3:

代码3:

public class prog {
    public static void main(String arg[]) {
        err ob1 = new err();
        ob1.func();
    }
}

class err {
    void func() {
        try {
            System.out.println("Inside try");
        } finally {
            System.out.println("Inside finally");
        }
    }
}

Output

输出量

Inside try
Inside finally

Explanation:

说明:

It is not necessary that we attach a catch block to a try block. Try statement can be associated with a finally statement directly.

不必将catch块附加到try块。 try语句可以直接与finally语句关联。



Code 4:

代码4:

import java.io.*;

public class prog {
    public static void main(String arg[]) {
        err ob1 = new err();
        ob1.func();
    }
}

class err {
    void func() throws IOException {
        try {
            System.out.println("Inside try");
        } catch (Exception e) {
            System.out.println("Inside catch");
        } finally {
            System.out.println("Inside finally");
        }
    }
}

Output

输出量

/prog.java:6: error: unreported exception IOException; must be caught or declared to be thrown
        ob1.func();
                ^
1 error

Explanation:

说明:

If a function is said to throw any checked exception, then that checked exception must be caught by the caller. The statement 'void func() throws IOException' clearly states that the function can throw an IOException that must be handled by the caller. The error can be corrected by enclosing the 'ob1.func()' statement in a try-catch block.

如果说一个函数抛出了任何已检查的异常,则调用者必须捕获该已检查的异常。 声明“ void func()throws IOException ”清楚地表明该函数可以抛出IOException,必须由调用者处理。 可以通过在try-catch块中包含' ob1.func() '语句来纠正该错误。



Code 5:

代码5:

import java.io.*;

public class prog {
    public static void main(String arg[]) {
        err ob1 = new err();
        ob1.func();
    }
}

class err {
    void func() throws RuntimeException {
        try {
            System.out.println("Inside try");
            try {
                int[] a = new int[10];
                int c = 10;
                a[c] = 0;
            }
        } catch (Exception e) {
            System.out.println("Inside catch");
        }

    }
}

Output

输出量

/prog.java:14: error: 'try' without 'catch', 'finally' or resource declarations
            try {
            ^
1 error

Explanation:

说明:

Every try block must have an associated catch or finally block. In the code, the inner try block does not have an associated catch or finally block.

每个try块必须具有关联的catch或finally块。 在代码中,内部try块没有关联的catch或finally块。



翻译自: https://www.includehelp.com/java/examples-on-exceptional-handing-in-java.aspx

java 批量处理 示例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值