我们可以在 Java 中重写 catch 块吗?
描述
当一段特定方法中的代码抛出异常,并使用 try-catch 对进行处理时。如果我们从另一个方法调用此方法,并且调用行包含在 try-catch 对中。现在,如何通过调用方法的 catch 块来覆盖 catch 块。
当方法中的代码抛出异常(编译时)时,我们必须要么使用 try-catch 对进行处理,要么使用 throws 关键字将其抛出(推迟)到调用方法,否则会发生编译时错误。
在下面的 Java 示例中,readFile() 方法中的代码生成 FileNotFoundException,而我们没有处理它。
示例
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class ExceptionExample{ public static String readFile(String path){ String data = null; Scanner sc = new Scanner(new File("E://test//sample.txt")); String input; StringBuffer sb = new StringBuffer(); sb.append(sc.next()); data = sb.toString(); return data; } public static void main(String args[]) { String path = "E://test//sample.txt"; readFile(path); } }
Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.
编译时错误
编译时,上述程序会生成以下编译时错误。
ExceptionExample.java:7: error: unreported exception FileNotFoundException; must be caught or declared to be thrown Scanner sc = new Scanner(new File("E://test//sample.txt")); ^ 1 error
要解决此错误,我们需要使用 try-catch 对处理异常,例如:
示例
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class ExceptionExample{ public static String readFile(String path){ String data = null; try { Scanner sc = new Scanner(new File("E://test//sample.txt")); String input; StringBuffer sb = new StringBuffer(); sb.append(sc.next()); data = sb.toString(); } catch (FileNotFoundException ex) { System.out.println("exception occurred"); } return data; } public static void main(String args[]) { String path = "E://test//sample.txt"; readFile(path); } }
输出
exception occurred
或者,使用 throws 关键字抛出它。如果您这样做,异常将被推迟到此方法的调用行,即错误现在出现在该行。
示例
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class ExceptionExample{ public static String readFile(String path)throws FileNotFoundException { String data = null; Scanner sc = new Scanner(new File("E://test//sample.txt")); String input; StringBuffer sb = new StringBuffer(); sb.append(sc.next()); data = sb.toString(); return data; } public static void main(String args[]) { String path = "E://test//sample.txt"; readFile(path); } }
编译时错误
ExceptionExample.java:17: error: unreported exception FileNotFoundException; must be caught or declared to be thrown readFile(path); ^ 1 error
要解决此问题,您需要包装调用行或使用 throws 关键字再次抛出异常。
public static void main(String args[]) { String path = "E://test//sample.txt"; OverridingException obj = new OverridingException(); try { readFile(path); }catch(Exception ex) { System.out.println("Exception occured"); } }
也就是说,如果您在源方法(最初生成异常的方法)中使用 try-catch 对处理异常,则无需在调用方法中再次处理它。在两个地方都使用 try-catch 是没有意义的。
执行两个 catch 块中内容的唯一方法是重新抛出异常。
重新抛出异常
当异常在 catch 块中被捕获时,您可以使用 throw 关键字(用于抛出异常对象)重新抛出它。
重新抛出异常时,您可以原样抛出相同的异常,而无需调整它,例如:
try { int result = (arr[a])/(arr[b]); System.out.println("Result of "+arr[a]+"/"+arr[b]+": "+result); } catch(ArithmeticException e) { throw e; }
或者,将其包装在新的异常中并抛出它。当您将捕获的异常包装在另一个异常中并抛出它时,这被称为异常链或异常包装,通过这样做,您可以调整您的异常,抛出更高级别的异常来维护抽象。
try { int result = (arr[a])/(arr[b]); System.out.println("Result of "+arr[a]+"/"+arr[b]+": "+result); } catch(ArrayIndexOutOfBoundsException e) { throw new IndexOutOfBoundsException(); }
示例
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class ExceptionExample{ public static String readFile(String path){ String data = null; try { Scanner sc = new Scanner(new File("E://test//sample.txt")); String input; StringBuffer sb = new StringBuffer(); sb.append(sc.next()); data = sb.toString(); }catch (FileNotFoundException ex){ System.out.println("Exception occurred: source method"); throw ex; } return data; } public static void main(String args[]) { String path = "E://test//sample.txt"; try { readFile(path); }catch(Exception ex) { System.out.println("Exception occurred: calling method"); } } }
输出
Exception occurred: source method Exception occurred: calling method