Can someone please help me with something that seems simple but I've never really got my head round. Exceptions! I know how to write an exception but countless code samples with variables foo and bar doesn't help me in the slightest to see how these are actually used in real life scenarios.

Is it where there is potential to stop the process of execution? I have to confess I never use them, I just don't know how to use them. I'm sure they are important though. I guess if something is going to break, I just let it break, it's going to throw an error to the screen anyway and stop the execution. Is the point of an exception to not have an error of such appear and catch the error, and pass a message to the view to output? Then this lends my next questions, how do I know what parts of my code are likely to break? Should I wrap everything in exceptions handles? How extensively should these be throughout my code?

Sorry for something that seems like it could be figured out easily, I've just never managed to properly understand their usage. I'd appreciate if someone could point me in the correct direction at least, some real examples of which code to wrap would also be much appreciated. Thanks

  • Welcome to Super User! General programming questions are off-topic on this site. You may have more luck at Stack Overflow, though your question may be too broad their specifications. If you wish for your post to be migrated, flag your question and explain to a moderator what you wish to be done. – Excellll May 30 '14 at 1:04

I think exceptions are there for those times when a program enters a state that programmer considers wrong, to alert the program of this error and perhaps continue execution in an alternate way (catch) or just clean-up.

Stuff that is simple and should always work, like simple arithmetic with small values, string operations, or simple function calls I never handle except if they could be recursive or depend on some unstable input.

I typically use try/catch when the program depends heavily on user actions or other I/O (files, databases, sockets) - variables whose values I can not predict with very high accuracy(if they depend on remote services, could exhaust memory, or time-out)

If you haven't yet, try programming some Java to see what makers of it consider cases when exceptions are needed.

Here's an example: I need the current currency rate for €->$ conversion

  • is data already in cache and not older than 5 minutes? Then use it. If not:
  • try to use a web service to get the latest factor from a website.
  • if the requested value can not be found in response or is way off I throw an exception
  • in catch I would
    • try to get last saved factor from database/file
    • if data from db could for some reason not be returned I throw again
    • catch the exception, wrote database problem to the log (perhaps in another try/catch in case disk was full and log could not be written) and
      • use some constant factor that would be in code (or write "sorry cannot get current conversion data"
| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.