Exception Handling

What is the need to go for nested try-catch like as below
void f()
{
try
{
//Some code
try
{
//Some code
}
catch(ExceptionA a)
{
//Some specific exception handling
}
//Some code
}
catch(...)
{
//Some exception handling
}
}//f
Well maybe you want to supress exception from one statement, while allowing normal exception handling in other causes. This is hard to tell due to... uninformativeness of given code.
Last edited on
Like @MiiNiPaa has stated it is difficult to say due to the lack of code you have supplied. I would advise that if your code in a single block is too long, i.e. over a screen length then you should consider refactoring to split the code into smaller separate functions, each with their own try catch blocks, as and when required.

You can have multiple catch blocks to catch different errors that are likely to arise from your code as well as the catch all (...) at the bottom:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void f()
{
   try
   {
      // some code
   }
   catch (ExceptionA& a)
   {
      // ExceptionA handler code
   }
   catch(...)
   {
      // generic exception handler code
   }
}
Last edited on
Topic archived. No new replies allowed.