Java中的多个捕获
为了捕获单个try块生成的不同异常,我们在Java中使用了多个catch块。
多捕获块的语法如下-
try {
}
catch(Exception1 | Exception2 | Exception3…..| ExceptionN exception_obj)
{
}让我们看看Java中用于多个catch块的程序-
示例
import java.util.Scanner;
public class Example {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
try {
int n = Integer.parseInt(sc.next());
System.out.println(n/0);
}
catch (ArithmeticException | NumberFormatException e) {
System.out.println("Exception Caught: " + e);
}
}
}输出取决于输入。
对于以下输入-
5
输出结果
对于以下输入-
Exception Caught: java.lang.ArithmeticException: / by zero
Hello
输出结果
Exception Caught: java.lang.NumberFormatException: For input string: "Hello"
热门推荐
10 读书祝福语女生短句简短
11 感谢同事新婚祝福语简短
12 感谢教官文案祝福语简短
13 员工聚会蛋糕祝福语简短
14 元宵发客户祝福语简短
15 给团队信心祝福语简短
16 医美的祝福语大全简短
17 异国姻缘的祝福语简短
18 兄妹拍照祝福语简短的话