grabner
grabner

Reputation: 1239

Exception handling of subprogram calling subprogram

I have a method which has a subprogram which is again calling a subprogram. The last subprogram can throw an exception. Who is responsible for catching the exception? Or who is able to catch the exception?

So are the exceptions passed up in the calling stack until a method catches them? Or is only the calling method responsible for catching the exception of the subprogram?

Upvotes: 1

Views: 191

Answers (1)

Sayse
Sayse

Reputation: 43300

All 3

Each program should handle its own errors, and then if any have a dependency on another, they should make sure they dont crash as a result of an error occuring in that dependency.

Arguably, the most important would be the program handling its own error since it may be used within other programs also.


Your main method shouldn't need to worry about what happened to make the final program crash ("sub sub method"), it only needs to know why the sub method crashed. If it does matter for some reason, then the reasoning why the final program crashed is probably down to different parameters to what you passed in to begin with. The final program will say it crashed because of X, middle program should note this, find it is because of parameter Y from the first program and only report that back to the first program (as required).

You need to avoid creating a dependency between the final program and the first program as this will lead to a nightmare of maintenance as the final program is updated.

Upvotes: 1

Related Questions