简体   繁体   English

在 Java 中抛出自定义异常

[英]Throwing custom exceptions in Java

Why do I need to wrap my thrown custom exceptions with try / catch whilst trying to throw them, but I don't have to do that for generic exceptions?为什么我需要在try抛出自定义异常时用try / catch包装它们,但我不必为通用异常执行此操作? Like in the example, my Exception subclass :就像在示例中一样,我的Exception子类:

public class MyException extends Exception {
    public MyException(String msg) {
        super(msg);
    }
}

Throwing exceptions :抛出异常:

public class Exe {

    private static void testex(String test) {
        if (null!=test) {
            throw new UnsupportedAddressTypeException();
        } else {//Removing try/catch block results in compile failure
          try {
            throw new MyException("message");
          } catch (MyException e) {
            e.printStackTrace();
          }
        }
    }
}

UnsupportedAddressTypeException is a subclass of RuntimeException, and from the JavaDoc: UnsupportedAddressTypeException 是 RuntimeException 的子类,来自 JavaDoc:

RuntimeException is the superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine. RuntimeException 是那些在 Java 虚拟机正常运行期间可以抛出的异常的超类。

A method is not required to declare in its throws clause any subclasses of RuntimeException that might be thrown during the execution of the method but not caught.方法不需要在它的 throws 子句中声明在方法执行期间可能抛出但未被捕获的 RuntimeException 的任何子类。

If your exception extends java.lang.Exception, you must catch it (or rethrow).如果您的异常扩展了 java.lang.Exception,您必须捕获它(或重新抛出)。 If it extends java.lang.RuntimeException, you are not required to do so.如果它扩展了 java.lang.RuntimeException,则不需要这样做。 You will find that this is true for all standard exceptions as well.您会发现对于所有标准异常也是如此。

edit Changed the words must not to not required to编辑更改了的话一定不要不需要

Your static method should declare你的静态方法应该声明

private static void testex(String test) throws MyException

if you want the method to throw it (and not to catch and handle it internally).如果您希望该方法抛出它(而不是在内部捕获和处理它)。

From the Java Tutorial :来自Java 教程

Valid Java programming language code must honor the Catch or Specify Requirement.有效的 Java 编程语言代码必须遵守捕获或指定要求。 This means that code that might throw certain exceptions must be enclosed by either of the following:这意味着可能抛出某些异常的代码必须用以下任一方式括起来:

  • A try statement that catches the exception.捕获异常的 try 语句。 The try must provide a handler for the exception, as described in Catching and Handling Exceptions. try 必须为异常提供处理程序,如捕获和处理异常中所述。

  • A method that specifies that it can throw the exception.一种指定它可以抛出异常的方法。 The method must provide a throws clause that lists the exception, as described in Specifying the Exceptions Thrown by a Method.该方法必须提供一个列出异常的 throws 子句,如指定方法引发的异常中所述。

Code that fails to honor the Catch or Specify Requirement will not compile.不遵守 Catch 或 Specify Requirement 的代码将无法编译。 Not all exceptions are subject to the Catch or Specify Requirement.并非所有例外都受捕获或指定要求的约束。

[...] [...]

Runtime exceptions are not subject to the Catch or Specify Requirement.运行时异常不受捕获或指定要求的约束。 Runtime exceptions are those indicated by RuntimeException and its subclasses.运行时异常是由 RuntimeException 及其子类指示的异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM