Extending Throwable in Java

前端 未结 7 1376
长发绾君心
长发绾君心 2020-12-16 10:47

Java lets you create an entirely new subtype of Throwable, e.g:

public class FlyingPig extends Throwable { ... }

Now, very

7条回答
  •  余生分开走
    2020-12-16 10:57

    As this question has evolved, I see that I misunderstood the point of the original question, so some of the other answers are probably more relevant. I will leave this answer up here, since it may still be helpful to others who have a similar question, and find this page while searching for an answer to their question.

    There is nothing wrong with extending Throwable to be able to throw (and handle) custom exceptions. However, you should keep the following points in mind:

    • Using the most specific exception possible is a great idea. It will allow the caller to handle different types of exceptions differently. The class hierarchy of the exception is important to keep in mind, so your custom exception should extend another type of Throwable that is as close to your exception as possible.
    • Extending Throwable might be too high-level. Try extending Exception or RuntimeException instead (or a lower-level exception that is closer to the reason you are throwing an exception). Keep in mind the difference between a RuntimeException and an Exception.
    • A call to a method that throws an Exception (or a subclass of Exception) will need to be wrapped in a try/catch block that is capable of dealing with the exception. This is good for cases when you expect things to go wrong, due to circumstances that may be out of your control (eg, the network being down).
    • A call to a method that throws a RuntimeException (or a subclass of it) does not need to be wrapped in a try/catch block that can handle the exception. (It certainly could be, but it doesn't need to be.) THis is more for exceptions that really shouldn't be expected.

    So, suppose you have the following exception classes in your code base:

    public class Pig extends Throwable { ... }
    public class FlyingPig extends Pig { ... }
    public class Swine extends Pig { ... }
    public class CustomRuntimeException extends RuntimeException { ... }
    

    And some methods

    public void foo() throws Pig { ... }
    public void bar() throws FlyingPig, Swine { ... }
    // suppose this next method could throw a CustomRuntimeException, but it
    // doesn't need to be declared, since CustomRuntimeException is a subclass
    // of RuntimeException
    public void baz() { ... } 
    

    Now, you could have some code that calls these methods like this:

    try {
        foo();
    } catch (Pig e) {
        ...
    }
    
    try {
        bar();
    } catch (Pig e) {
        ...
    }
    
    baz();
    

    Note that when we call bar(), we can just catch Pig, since both FlyingPig and Swine extend Pig. This is useful if you want to do the same thing to handle either exception. You could, however, handle them differently:

    try {
        bar();
    } catch (FlyingPig e) {
        ...
    } catch (Swine e) {
        ...
    }
    

提交回复
热议问题