When to use try{} catch{<exeption>} block)?

I am reading information about exception handling and I wonder when should I write the try{} catch{<exception>} block.
Until know I only use it when the IDE suggested me to do it. But I am wondering if there are specific cases where would be a good practice to use it.

Thanks
Last edited on
you use then anywhere you know that your program could crash or malfunction due to some (usually external) failure. A simple example would be math errors like division by zero, sqrt of negative not using the complex type, overflow, and so on. When you identify the possible issue, you can trap the error and handle it somehow (tell the user, recover from the problem) or in some code gracefully shutdown and end your process. You may use them if your code touches hardware, which can be uninstalled or have a hardware failure, for another example, like an external usb device.

You also occasionally see abuse these blocks to trigger code intentionally when no actual failure happened. Hopefully people have stopped doing this for the most part.
Last edited on
A word of caution: while try/catch can be very useful, they can also be very dangerous. Consider:

1
2
3
4
5
6
7
8
extern void f();
int x = 1, y = 2;
if (x == 1) {
    f();
} else {
    x = 2;
}
y=1;

Question: which statement is executed after line 4?
Answer: No frickin clue! If f() throws an exception then there's no way to know until runtime.

People figured out in the 1950's or 1960's that structured code was a good idea. You need to be able to see how control flows through a program. If you aren't careful, your C++ exceptions can turn your code into Eniac-era spaghetti code.

If your code throws an exception, always document that fact. And for goodness sake, catch the exception if you're going to throw it.
> A word of caution: while try/catch can be very useful, they can also be very dangerous.
> Consider: ...
> Question: which statement is executed after line 4?


A word of caution: while functions can be very useful, they can also be very dangerous. Consider:
1
2
3
4
5
6
7
int foo( int a ) 
{
      if( a < 100 ) 
          return -1 ;
      else
          return a - 100 ;
}

Question: which statement is executed after line 4?

You need to be able to see how control flows through a program. If you aren't careful, functions can turn your code into Eniac-era spaghetti code.

Moral of the story: In general, write one gargantuan main() which does everything that the program is supposed to do. In particular, never, ever, write a library.
puertas12 wrote:
I wonder when should I write the try{} catch{<exception>} block.

Catch where the program can handle the error meaningfully. Don't catch at all if your error handling strategy is to terminate the process (e.g. you're a tab in Chrome browser).

For example, when a user opens a file in Notepad++, there's a try{}catch in the code that is executed from the UI. The function called in the try block calls other functions, they call other functions, eventually open and read the selected file, then they call yet other functions (from a different library even) that populate internal data structures with the contents of the file, preparing it for editing. If one of the calls allocating a new row in the buffer runs out of memory and throws std::bad_alloc, the exception rolls back all these actions: deallocates all other rows that were allocated so far, destroys buffers, closes the file, and finally reaches that try/catch, which then brings up the pop-up informing the user the file could not be loaded, and goes back to waiting for user interaction with the UI.

In fact many batch, interactive, and server applications have just one try/catch, in their main loop, to undo whatever broke when handling a work item/user action/client request and proceed to the next one safely.
Last edited on
Topic archived. No new replies allowed.