[英]How to catch Exceptions occured in Catch block in Java
我需要处理由Java中的Catch块代码引发的异常
您可以在方法或块中的任何位置使用try catch块,因此您也可以在catch块中编写try catch。
try {
// master try
}catch(Exception e){
// master catch
try {
// child try in master catch
}catch(Exception e1){
// child catch in master catch
}
}//master catch
例如,“处理”异常:
try
{
// try do something
}
catch (Exception e)
{
System.out.println("Caught Exception: " + e.getMessage());
//Do some more
}
更多信息请参阅:请参阅: https : //docs.oracle.com/javase/tutorial/essential/exceptions/catch.html
但是,如果您想在try catch中使用另一个catch,则可以执行以下操作:
try
{
//Do something
}
catch (IOException e)
{
System.out.println("Caught IOException: " + e.getMessage());
try
{
// Try something else
}
catch ( Exception e1 )
{
System.out.println("Caught Another exception: " + e1.getMessage());
}
}
小心嵌套的try / catch,当你的try catch变得复杂/大时,考虑将它拆分成自己的方法。 例如:
try {
// do something here
}
catch(IOException e)
{
System.out.println("Caught IOException: " + e.getMessage());
foo();
}
private void foo()
{
try {
// do something here (when we have the IO exception)
}
catch(Exception e)
{
System.out.println("Caught another exception: " + e.getMessage());
}
}
像通常的尝试/捕获情况那样做:
try{
throw new Exception();
}catch(Exception e1){
try{
throw new Exception();
}catch(Exception e2){
//do something
}
}
您可以在主catch块中添加新的try catch块。
try
{
int b=10/0;
}catch(ArithmeticException e)
{
System.out.println("ArithmeticException occurred");
try
{
int c=20/0;
}catch(ArithmeticException e1)
{
System.out.println("Another ArithmeticException occurred");
}
}
我认为最干净的方法是创建捕获异常发生在其体内的方法 。 但是,它可能非常依赖于您正在处理的代码的情况和类型。
你要问的一个例子是关闭一个在try
- catch
- finally
块中打开的Stream
。 例如:
package a;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class Main {
public static void main(String[] args) {
OutputStream out = null;
try {
out = new BufferedOutputStream(new FileOutputStream("temp.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
//TODO: Log the exception and handle it,
// for example show a message to the user
} finally {
//out.close(); //Second level exception is
// occurring in closing the
// Stream. Move it to a new method:
closeOutPutStreamResource(out);
}
}
private static void closeOutPutStreamResource(OutputStream out){
try {
out.close();
} catch (IOException e) {
// TODO: log the exception and ignore
// if it's not important
// OR
// Throw an instance of RuntimeException
// or one of it's subclasses
// which doesn't make you to catch it
// using a try-catch block (unchecked)
throw new CloseOutPutStreamException(e);
}
}
}
class CloseOutPutStreamException extends RuntimeException{
public CloseOutPutStreamException() {
super();
}
public CloseOutPutStreamException(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
public CloseOutPutStreamException(String message, Throwable cause) {
super(message, cause);
}
public CloseOutPutStreamException(String message) {
super(message);
}
public CloseOutPutStreamException(Throwable cause) {
super(cause);
}
}
这里我说明了在finally
块中发生第二级异常的情况,但同样可以应用于catch
块中发生的异常。
在我看来,编写诸如closeOutPutStreamResource
方法非常有用,因为它们打包了一个锅炉板代码,用于处理非常常见的异常,它们使代码更加优雅。
您也可以选择在closeOutPutStreamResource
catch
并记录异常,或将其throw
到程序的其他层。 但是将这些不重要的检查异常包装到RuntimeException
而不需要捕获会更优雅。
希望这会有所帮助。
而不是级联try / catch(就像在大多数其他答案中一样),我建议你调用另一个方法 ,执行所需的操作。 通过这种方式,您的代码将更容易维护 。
在这个方法中,放一个try / catch块来保护代码。
示例:
public int classicMethodInCaseOfException(int exampleParam) {
try {
// TODO
}
catch(Exception e)
{
methodInCaseOfException();
}
}
public int methodInCaseOfException()
{
try {
// TODO
}
catch(Exception e)
{
//TODO
}
}
当catch块抛出Exception时,没有必要使用嵌套的try-catch块,因为这里的所有答案都表明了这一点。 您可以使用try-catch包含调用方法来处理该异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.