When to catch an Exception

12 views
Skip to first unread message

Johnny Chang

unread,
Apr 25, 2011, 7:00:39 PM4/25/11
to UBC CPSC 210
I was just wondering when do you catch an exception and when do you
propogate it?

thanks

Paul Carter

unread,
Apr 25, 2011, 7:58:36 PM4/25/11
to ubc-cp...@googlegroups.com
In general, you will propagate the exception back through the call chain to the point in the code that is best able to deal with the error that led to the exception. At that point you catch the exception and deal with the problem as best you can. Note that in some situations, "dealing with the problem" might involve notifying the user that something critical has gone wrong (e.g. can't connect to the network) and then terminating the program, while in others we might be able to fix the problem (e.g. ask the user to re-enter some value when the original input was not valid) and continue.

Paul

Reply all
Reply to author
Forward
0 new messages