开发者

Is it good idea to propagate error to parent and then grand parent?

开发者 https://www.devze.com 2023-01-30 09:25 出处:网络
This is general question in software engg. Handling error at low level may be too much code. In other hand, handing error at top level may lead to loss of information.

This is general question in software engg. Handling error at low level may be too much code. In other hand, handing error at top level may lead to loss of information. What d开发者_运维知识库o you think?


It really depends where the program can handle the error, report it in a useful manner, and continue some useful processing.

In UI code I've written, while processing an event for example, I may call a private method and then some utility method that encounters an error. Handling it in the utility method would not make much sense since it may not be aware of the event that is being handled and all the methods of reporting the error to the user. The first level of the event handling is probably in a better situation to handle it and either stop processing or make the necessary adjustments.

On the other hand, if the utility method can legitimately handle the error for all situations that it will be used (maybe there's some retry logic or plan B logic), then it might be here.

But, for much of the UI code that I've done, it usually is passed up to a higher level since it's in a better position to handle it for the specific situation.


It all depends on what the requirements are.

In some cases you do wish to bubble up the exception to the caller, as the caller might be better equiped to handle the problem. Sending an email might not always be system critical, but it can be when it needs to tell you someone logged into your bank account.

On the other hand, you do not wish to bubble up exceptions that should be handles by the called method. If you were writing something that was to download a file via FTP, you do not wish to bother the caller with every dropped connection, if you intend to try again just a little later.

So again, this depends on the situation at hand, rather than a rule of thumb.

0

精彩评论

暂无评论...
验证码 换一张
取 消