Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid writing repeated code in catch blocks?

I am using QT 4.8 (C++) for desktop application project, and writing exception handling which is as follows :

void callerMethod()
{
  try
  {
   method1();
  }
  catch(Exception1& e)
  {
    // display critcal error message
    // abort application
  }
  catch(std::Exception& e)
  {
   // print exception error message
  }
  catch(...)
  {
   // print unknown exception message
  } 
}

void method1()
{
  try
  {
   // some initializations
   // some operations (here exceptions can occur)
   // clean-up code (for successful operation i.e no exception occurred)
  }
  catch(Exception1& e)
  {
   // clean-up code
   throw e;
  }
  catch(Exception2& e)
  {
   // clean-up code
   throw e;
  }
  catch(Exception3& e)
  {
   // clean-up code
   throw e;
  }
  catch(...)
  {
   // clean-up code
   throw;
  }
}

So my question do I need to write the clean-up code in every catch block? Is there any way I can avoid writing repeated code?

NOTE:: [ In method1() ] I want to re-throw exceptions which occurred to my caller.So I can not catch them in single catch block, because then type information will be lost.

like image 310
Anwar Shaikh Avatar asked Apr 21 '12 06:04

Anwar Shaikh


People also ask

How do I stop multiple try catch blocks?

To avoid writing multiple try catch async await in a function, a better option is to create a function to wrap each try catch. The first result of the promise returns an array where the first element is the data and the second element is an error. And if there's an error, then the data is null and the error is defined.

Is it good practice to write code in catch block?

Only error prone code is within the try block and the handling of that error is written in the catch block. It is used to handle run time exceptions. It is a good practice to write the code in try block which may generate an error , so, that the code doesn't terminate abruptly.

How many times you can write catch block?

No, we can write multiple catch block but only one is executed at a time. No. Only one catch block execute at a time. We can have multiple catch blocks but only one catch block will be execute at a time.


2 Answers

Method1 can be much simplified by two concepts:

  1. RAII. Put any clean-up code into destructors, and the clean-up code will be centralized.
  2. Use the unqualified throw, and you won't need to know about the type of exception thrown.

So, method1() should look like:

void method1()
{
     // some initializations of RAII objects
     // some operations (here exceptions can occur)
}

The first catch clause in callerMethod can be removed if you derive Exception1 from std::exception, since the what() method is virtual.

like image 198
thiton Avatar answered Oct 21 '22 06:10

thiton


You should throw exceptions as low as possible and catch them as high as possible in the call chain. This automatically leads to less code duplication, and centralizes error handling. You are throwing/catching all in one place, which seems a bit ... forced.

I often do this kind of thing (especially for program-ending exceptions:

int main()
try
{
    function_calls_that_may_throw();
    // ...
}
catch(my_exception& e)
{
    e.do_exception_stuff();
}
catch(std::exception& e)
{
    std::cout << e.what();
}
catch(...)
{
    std::cout << "Something bad happened.\n";
}

This is only possible for throwing exceptions you don't plan on handling better or retrying the failed operation or something.

The pro of this approach is that all/most error handling code is at the top-level of your program, and all the functions in the call chain don't have to worry one bit about this stuff, all they do is throw an exception when they feel like it.

like image 42
rubenvb Avatar answered Oct 21 '22 07:10

rubenvb