Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
try/catch块(Java)中引发的异常_Java_Try Catch - Fatal编程技术网

try/catch块(Java)中引发的异常

try/catch块(Java)中引发的异常,java,try-catch,Java,Try Catch,我几周前才开始学习Java。我从我的课本上读到,它说:如果在执行一个代码块的中途发生异常,该代码块被一个“try”块包围,后面跟着几个“catch”子句(它们有自己的代码块),try块的其余部分将被跳过,如果有一个catch子句与异常类型匹配,然后将执行与catch子句关联的块。 但是,如果不存在匹配的catch子句,会发生什么情况?不会执行任何操作或发生任何特定情况?我知道这只是一个简单的问题,但我找不到任何答案。感谢您的帮助。如果不存在捕获指定异常的catch块,则会向上抛出错误(就像没有t

我几周前才开始学习Java。我从我的课本上读到,它说:如果在执行一个代码块的中途发生异常,该代码块被一个“try”块包围,后面跟着几个“catch”子句(它们有自己的代码块),try块的其余部分将被跳过,如果有一个catch子句与异常类型匹配,然后将执行与catch子句关联的块。
但是,如果不存在匹配的catch子句,会发生什么情况?不会执行任何操作或发生任何特定情况?我知道这只是一个简单的问题,但我找不到任何答案。感谢您的帮助。

如果不存在捕获指定异常的catch块,则会向上抛出错误(就像没有try/catch系列一样)。如果存在finally块,它当然仍将被执行。

如果不存在捕获指定异常的catch块,则会向上抛出错误(就像没有try/catch系列围绕它一样)。如果存在finally块,它当然仍将被执行。

我将尝试为您解释这一点

下面是引发异常的方法的示例:

public void anExceptionThrowingMethod() {
    throw new Exception("Uh oh an exception occurred!");
}
如果我们尝试这样调用此方法:

anExceptionThrowingMethod();
您的程序将崩溃,您将得到以下错误:

java.lang.IllegalArgumentException: Uh oh an exception occurred!
这是因为当我们调用该方法时,我们没有处理发生错误的情况。为此,我们使用
try{}catch{}
块:

try {
    anExceptionThrowingMethod();
} catch(Exception e) {
    System.out.println("We handled the exception!");
}
程序将不再崩溃,并将打印:

We handled the exception!
运行此代码时,异常引发方法将引发异常。catch块将捕获该异常,并打印出堆栈跟踪。异常引发方法之后不会执行任何代码:

try {
    anExceptionThrowingMethod();
    // Nothing will be executed after this
} catch(Exception e) {
    // Instead, this catch block will be executed
    System.out.println("We handled the exception!");
}
如果始终希望执行某些代码,即使发生异常,也可以使用finally块:

try {
    anExceptionThrowingMethod();
    // Nothing will be executed after this
} catch(Exception e) {
    // Instead, this catch block will be executed
    System.out.println("We handled the exception!");
} finally {
    // This block will always be executed, regardless of whether an exception has occurred.
}
如果存在多个异常类型,您可以捕获超级类
异常
,也可以单独处理每个异常类型:

try {
    manyExceptionThrowingMethod();
    // Nothing will be executed after this
} catch (InterruptedException e) {
    // Called when an InterruptedException occurs
    e.printStackTrace();
} catch (IllegalArgumentException e) {
    // Called when an IllegalArgumentException occurs
    e.printStackTrace();
} finally { 
    // This code will always be executed, regardless of whether an exception has occurred.
}

如果您不处理异常类型,当出现错误时,您的程序将崩溃

我将尝试为您解释这一点

下面是引发异常的方法的示例:

public void anExceptionThrowingMethod() {
    throw new Exception("Uh oh an exception occurred!");
}
如果我们尝试这样调用此方法:

anExceptionThrowingMethod();
您的程序将崩溃,您将得到以下错误:

java.lang.IllegalArgumentException: Uh oh an exception occurred!
这是因为当我们调用该方法时,我们没有处理发生错误的情况。为此,我们使用
try{}catch{}
块:

try {
    anExceptionThrowingMethod();
} catch(Exception e) {
    System.out.println("We handled the exception!");
}
程序将不再崩溃,并将打印:

We handled the exception!
运行此代码时,异常引发方法将引发异常。catch块将捕获该异常,并打印出堆栈跟踪。异常引发方法之后不会执行任何代码:

try {
    anExceptionThrowingMethod();
    // Nothing will be executed after this
} catch(Exception e) {
    // Instead, this catch block will be executed
    System.out.println("We handled the exception!");
}
如果始终希望执行某些代码,即使发生异常,也可以使用finally块:

try {
    anExceptionThrowingMethod();
    // Nothing will be executed after this
} catch(Exception e) {
    // Instead, this catch block will be executed
    System.out.println("We handled the exception!");
} finally {
    // This block will always be executed, regardless of whether an exception has occurred.
}
如果存在多个异常类型,您可以捕获超级类
异常
,也可以单独处理每个异常类型:

try {
    manyExceptionThrowingMethod();
    // Nothing will be executed after this
} catch (InterruptedException e) {
    // Called when an InterruptedException occurs
    e.printStackTrace();
} catch (IllegalArgumentException e) {
    // Called when an IllegalArgumentException occurs
    e.printStackTrace();
} finally { 
    // This code will always be executed, regardless of whether an exception has occurred.
}

如果您不处理异常类型,则当该错误发生时,您的程序将崩溃

您是否可以发布代码以便我们进行处理?请记住,
try catch
块可以有一个
finally
部分,该部分保证在块完成后始终执行,而不管块内发生了什么(不包括对
System.exit()
的调用或其他严重故障)。在我这方面有一点提示…如果您使用IntelliJ或eclipse等IDE,您可以让他们为您生成try-catch块,如果您调用的方法显式抛出异常(nullPointer等,可能不会)-例如,您不必担心任何未捕获的异常。您可以发布您的代码以便我们进行分类吗?请记住,
try-catch
块可以有一个
finally
部分,该部分保证始终在块完成后执行,而不管块内发生了什么(不包括对
System.exit()
的调用或其他严重故障)。在我这方面有一点提示…如果您使用IntelliJ或eclipse等IDE,您可以让他们为您生成try-catch块,如果您调用的方法显式抛出异常(nullPointer等,可能不会)-例如,您不必担心任何未被捕获的异常。