Exception 为什么创建异常层次结构比简单地在同一级别创建所有异常更好
我正在学习异常,我在某个地方读到,我们必须始终创建异常层次结构,而不是简单地在同一级别创建所有异常。我不明白为什么会这样 例如:Exception 为什么创建异常层次结构比简单地在同一级别创建所有异常更好,exception,Exception,我正在学习异常,我在某个地方读到,我们必须始终创建异常层次结构,而不是简单地在同一级别创建所有异常。我不明白为什么会这样 例如: Why This Exception --> ATMException --> 1. ATMAuthException --> 1.1 WrongATMCardException 1.2 WrongATMPinException
Why This
Exception --> ATMException --> 1. ATMAuthException --> 1.1 WrongATMCardException
1.2 WrongATMPinException
2. WithdrawException --> 2.1 InsufficientBalanceException
Not This
Exception --> ATMException --> 1. WrongATMCardException
2. WrongATMPinException
3. InsufficientBalanceException
try {
...
} catch (ATMAuthException e) {
...
}
假设您在下面创建了一个异常。如果您想捕获这两个异常并处理它们呢
public void foo1() throws WrongATMCardException, WrongATMPinException {}
public void foo2(){
try{
// do stuff..
}catch(WrongATMCardException ex){
foo3();
}catch(WrongATMPinException ex){
foo3();
}
}
public void foo3(){
System.out.println("ATM login failed. Throwing authentication failed exception");
// throw NO exception handles both. ???
}
但如果您像上面那样声明异常,您可以通过抛出atmautheexception
来处理异常,也可以轻松地处理、抛出异常
public void foo1() throws WrongATMCardException, WrongATMPinException {}
public void foo2(){
try{
// do stuff..
}catch(ATMException ex){
foo3();
}
}
public void foo3(){
System.out.println("ATM login failed. Throwing authentication failed exception");
throw new ATMException(); //single exception that handles all children.
}
在其他情况下,可以逐个抛出所有异常。但若你们有10,20个异常,基本上都是关于ATM异常的。您必须到处抛出并捕获这些异常,这会在代码的复杂性级别之后发生。假设您在下面创建了一个异常。如果您想捕获这两个异常并处理它们呢
public void foo1() throws WrongATMCardException, WrongATMPinException {}
public void foo2(){
try{
// do stuff..
}catch(WrongATMCardException ex){
foo3();
}catch(WrongATMPinException ex){
foo3();
}
}
public void foo3(){
System.out.println("ATM login failed. Throwing authentication failed exception");
// throw NO exception handles both. ???
}
但如果您像上面那样声明异常,您可以通过抛出atmautheexception
来处理异常,也可以轻松地处理、抛出异常
public void foo1() throws WrongATMCardException, WrongATMPinException {}
public void foo2(){
try{
// do stuff..
}catch(ATMException ex){
foo3();
}
}
public void foo3(){
System.out.println("ATM login failed. Throwing authentication failed exception");
throw new ATMException(); //single exception that handles all children.
}
在其他情况下,可以逐个抛出所有异常。但若你们有10,20个异常,基本上都是关于ATM异常的。您必须到处抛出并捕获这些异常,这在代码的复杂性级别之后才会发生。如果您创建分层异常,则可以在
try{}catch()
语句中立即捕获它们,而不必对每个异常单独执行此操作
例如:
Why This
Exception --> ATMException --> 1. ATMAuthException --> 1.1 WrongATMCardException
1.2 WrongATMPinException
2. WithdrawException --> 2.1 InsufficientBalanceException
Not This
Exception --> ATMException --> 1. WrongATMCardException
2. WrongATMPinException
3. InsufficientBalanceException
try {
...
} catch (ATMAuthException e) {
...
}
比
try {
...
} catch (WrongATMCardException | WrongATMPinException e) {
...
}
如果创建分层异常,则可以在
try{}catch()
语句中立即捕获它们,而不必分别对每个异常执行此操作
例如:
Why This
Exception --> ATMException --> 1. ATMAuthException --> 1.1 WrongATMCardException
1.2 WrongATMPinException
2. WithdrawException --> 2.1 InsufficientBalanceException
Not This
Exception --> ATMException --> 1. WrongATMCardException
2. WrongATMPinException
3. InsufficientBalanceException
try {
...
} catch (ATMAuthException e) {
...
}
比
try {
...
} catch (WrongATMCardException | WrongATMPinException e) {
...
}
是的,这是创建干净代码的一个用例。如果你能推荐一些其他的用例会更好。是的,这是创建干净代码的一个用例。如果你能建议一些其他的用例,那就更好了。