Catching and handling exceptions

A general technique to catch exceptions is to enclose any logic in a try-catch block. This is the easiest way to ensure that unexpected errors are handled:

try
# do something that may possible raise an error
catch ex
# recover from failure depending on the type of condition
end

A common question, however, is where this try-catch block should be placed. Of course, we could have just wrapped every single line of code but that would be impractical. After all, not every line of code would throw an error. 

We do want to be smart about choosing where to catch exceptions. We know that adding exception handling increases the code size. Additionally, every line of code requires maintenance. Ironically, the less code we write, the less chance of introducing bugs. After all, we should not introduce more problems by trying to catch problems, right?

Next, we will look at what kind of scenarios we should consider doing error handling for.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.156.140