首页 > Java > java教程 > 正文

Java 中的多个 Catch 块

王林
发布: 2024-08-30 15:11:46
原创
471 人浏览过

我们可以通过在 java 中插入 try-catch 块来处理代码中的异常。在本文中,我们将了解如何通过使用多个 catch 块或 catch 块的多个参数来处理单个 try 块的多个异常。

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

语法

Java 中多个 Catch 块的语法如下:

语法 1:简单的 try-catch 块

try
{
//code snippet which might responsible for exceptions
}
catch (<type_of_exception> <name_of_exception_object>)‏
{
//here we handle exceptions
}
登录后复制

语法 2:带有多个 catch 块的 try-catch 块

try
{
//code snippet which might responsible for exceptions
}
catch (<type_of_exception> <name_of_exception_object>)‏ //catch block 1
{
//here we handle exceptions
}
catch (<type_of_exception> <name_of_exception_object>)‏ //catch block 2
{
//here we handle exceptions
}
catch (<type_of_exception> <name_of_exception_object>)‏ //catch block 3
{
//here we handle exceptions
}
登录后复制

语法 3:try-catch 块,其中多个 catch 语句作为 catch 块的参数

try
{
//code snippet which might responsible for exceptions
}
catch (<type_of_exception1> | <type_of_exception2> | <type_of_exception3> <name_of_exception_object>)‏
{
//here we handle exceptions
}
登录后复制

Catch 块在 Java 中如何工作?

首先,我们需要在移动到 catch 块之前检查 try 块。

1.尝试阻止

它包含可能导致异常的代码片段。因此,每当您怀疑代码可能导致异常时,请将该代码片段放入 try 块中。 try 块至少需要一个 catch 块。

2.捕捉块

它包含处理编写在其相应 try 块内的代码可能发生的异常的代码片段。现在如果有多个异常,如何处理?

您可以采取两种方式:

  • 对 Try 块使用多个 Catch: 在这里,您可以在对应的 try 块的多个 catch 块下处理多个异常。程序流程会自动一一来到catch块,然后根据异常找到对应的catch块并进行相应的处理。
  • 在 Catch 块的参数中使用多个 Catch 语句:此功能来自 Java 7。在这里,您可以在单个 catch 块(用竖线符号分隔)下处理相应 try 块的多个异常。程序流程会自动来到catch块,然后在try块中找到catch块的参数中对应的异常,并进行相应的处理。

构造函数

我们还可以在构造函数中编写 try-catch 块,它将按预期工作。在下面的示例中,我们将在 try 构造函数中编写代码:try 块将生成算术异常,并在 catch 块中处理。

public class MultipleCatchBlockInConstructor
{
MultipleCatchBlockInConstructor()
{
try
{
int num=6/0;
}
catch(ArrayIndexOutOfBoundsException excp)
{
System.out.println("Exception is : "+excp);
}
catch(ArithmeticException excp)
{
System.out.println("Exception is : "+ excp);
}
}
public static void main(String[] args) {
MultipleCatchBlockInConstructor mc=new MultipleCatchBlockInConstructor();
}
}
登录后复制

输出:

Java 中的多个 Catch 块

Java 中的多个 Catch 块示例

在本节中,我们将讨论许多代码示例。您应该自己尝试此代码,并将输出与给定的输出进行比较。

示例#1

这是一个多 catch 块的简单示例,我们编写了一条语句,该语句将在 try 块内生成异常,该异常将由三个 catch 块中相应的 catch 块处理。

代码:

public class MultipleCatchBlockInJava {
public static void main(String[] args) {
try{
System.out.println("start of try block");
int num = 6/0;
}
catch(ArithmeticException e)
{
System.out.println("divide by zero exception ");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("ArrayIndexOutOfBounds Exception occurs");
}
catch(Exception e)
{
System.out.println("Any other excpetion");
}
System.out.println("end of try-catch block");
}
}
登录后复制

输出:

Java 中的多个 Catch 块

示例#2

在此示例中,我们通过 try 和 catch 块说明了数组的越界异常处理。

代码:

public class MultipleCatchBlockInJava {
public static void main(String[] args) {
try{
System.out.println("start of try block");
int arr[]=new int[6];
System.out.println(arr[12]); //we want to access 12th element of array which does not exist as size is of 6
}
catch(ArithmeticException e)
{
System.out.println("divide by zero exception");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Array IndexOutOfBounds Exception occurs");
}
catch(Exception e)
{
System.out.println("Any other exception");
}
System.out.println("end of try-catch block");
}
}
登录后复制

输出:

Java 中的多个 Catch 块

示例 #3

在此示例中,我们将了解 catch 块如何处理空指针。另外需要注意的是,try 块中存在多种异常情况,但是一旦程序流到达第一个异常生成语句(这里为空指针异常),它会立即移出 try 块并在 catch 块中搜索异常处理程序。 try 块内的其他异常生成语句(此处为数组索引越界异常)将被忽略。

代码:

public class MultipleCatchBlockInJava {
public static void main(String[] args) {
try{
System.out.println("start of try block");
String str=null;
System.out.println(str.length());  //we want to get length of a null string
int arr[]=new int[6];
System.out.println(arr[12]); //we want to access 12th element of array which does not exist as size is of 6
}
catch(ArithmeticException e)
{
System.out.println("divide by zero exception");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Array IndexOutOfBounds Exception occurs");
}
catch(NullPointerException e)
{
System.out.println("Null pointer Exception occurs");
}
System.out.println("end of try-catch block");
}
}
登录后复制

输出:

Java 中的多个 Catch 块

示例#4

在此示例中,我们将检查单个 catch 块的多个参数如何处理异常。

代码:

public class MultipleCatchBlockInJava {
public static void main(String[] args) {
try{
System.out.println("start of try block");
String str=null;
System.out.println(str.length());  //we want to get length of a null string
int arr[]=new int[6];
System.out.println(arr[10]); //we want to access 10th element of array which does not exist as size is of 6
}
catch(ArithmeticException | ArrayIndexOutOfBoundsException | NullPointerException e )
{
System.out.println("Code is throwing " + e);
}
System.out.println("end of try-catch block");
}
}
登录后复制

输出:

Java 中的多个 Catch 块

结论

我们从java编程角度对多个catch块的学习到此结束。总结一下:每当您需要处理多个异常时,您可以将它们写入多个 catch 块中。或者,对于 java 7 及更高版本,您可以在参数单个 catch 块中编写多个异常,程序流将自动选择它。另外,为了更好地理解,您应该自己编写代码,这一点很重要。

以上是Java 中的多个 Catch 块的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!