代码改变世界

Effective Java 59 Avoid unnecessary use of checked exceptions

2014-04-23 17:50  小郝(Kaibo Hao)  阅读(421)  评论(0编辑  收藏  举报

The burden is justified if the exceptional condition cannot be prevented by proper use of the API and the programmer using the API can take some useful action once confronted with the exception.

ask yourself how the programmer will handle the exception.

   

Is this the best that can be done?

} catch(TheCheckedException e) {

throw new AssertionError(); // Can't happen!

}

How about this?

} catch(TheCheckedException e) {

e.printStackTrace(); // Oh well, we lose.

System.exit(1);

}

   

Principle

  1. If the programmer using the API can do no better an unchecked exception would be more appropriate. The checked nature of the exception provides no benefit to the programmer, but it requires effort and complicates programs.
  2. Turning a checked exception into an unchecked exception is to break the method that throws the exception into two methods, the first of which returns a boolean that indicates whether the exception would be thrown.
     

// Invocation with checked exception

try {

obj.action(args);

} catch(TheCheckedException e) {

// Handle exceptional condition

...

}

   

to this:

// Invocation with state-testing method and unchecked exception

if (obj.actionPermitted(args)) {

obj.action(args);

} else {

// Handle exceptional condition

...

}

   

If you suspect that the simple calling sequence will be the norm, then this API refactoring may be appropriate. The API resulting from this refactoring is essentially identical to the state-testing method API in Item 57 and the same caveats apply: if an object is to be accessed concurrently without external synchronization or it is subject to externally induced state transitions, this refactoring is inappropriate, as the object's state may change between the invocations of actionPermitted and action . If a separate actionPermitted method would, of necessity, duplicate the work of the action method, the refactoring may be ruled out by performance concerns.