Code Design: How to avoid program control by catching an exception of a subroutine? -


today faced following problem @ our company: have 2 methods

void a() {...} // can throw timeout exception void b() {   try {     a();   } catch(timeoutexception e) {     ... // continue solving problem way   } } 

method b calls a internally. if method a throws timeout exception, method b catches , continues solving problem algorithm.

if consider method a isolated, throwing exception makes perfect sense. however, if consider method b, catching exception controlling logic looks code smell.

question: how can 1 improve situation?

at first glance, 1 think of predicate method pre-check whether method a can solve problem or not. in our situation not possible, because trying solve problem turns out if method a can or not.

the smell possibly stems fact trying interpret same fact (exception thrown) error (isolated a) part of business flow (a called in b).

one of ways of making explicit introduce parameter:

void a( bool throwsontimeout ) {...}  

this way behavior depends on caller expects.


Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -