[英]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.