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 妈妈生日祝福语简短励志