Exception and Inheritance in Java

后端 未结 5 1943
旧巷少年郎
旧巷少年郎 2021-01-04 12:59

Suppose we have this problem

public class Father{
    public void method1(){...}
}

public class Child1 extends Father{
    public void method1() throws Exce         


        
5条回答
  •  生来不讨喜
    2021-01-04 13:24

    Depends on what throws the Exception in Child1. If its some preconditions etc, you can always use any of the subclasses of RuntimeException such as IllegalArgumentException.

    However, if there is some sort of CheckedException, then logic suggests that you should handle it that method itself and bubble up the message in some other way.

    I think the general rule of thumb is that

    if you know how to handle it.. use a checked exception else unchecked exception

提交回复
热议问题