9

//I have written code in Catch Block

 try {

 } catch(Excepetion ex) {
        // I have written code here If Exception Occurs then how to handle it.
 }
kelalaka
  • 5,064
  • 5
  • 27
  • 44
Atul Phadtare
  • 555
  • 3
  • 7
  • 13
  • There are several correct answers below, but I'd probably revisit the design of my code to ensure than an exception is not thrown in a catch, or at a minimum abstract that out to another method. Multiple ```try/catch/finally``` blocks nested in one another is a code smell--at least it is to me :). – Jduv Nov 28 '12 at 04:58

7 Answers7

8

You can put a try catch inside the catch block, or you can simply throw the exception again. Its better to have finally block with your try catch so that even if an exception occurs in the catch block, finally block code gets executed.

try
  {
  }
catch(Excepetion ex)
  {
     try
        {
        }
     catch
        {
        }
   //or simply throw;
  }
finally
{
  // some other mandatory task
}

Finally block may not get executed in certain exceptions. You may see Constrained Execution Regions for more reliable mechanism.

Community
  • 1
  • 1
Habib
  • 219,104
  • 29
  • 407
  • 436
  • A nested try catch statement seems like bad design. If throwing an exception fails you have bigger bugs to fry. Finally will always happen so its a bad place to put code that only should happen if the catch statement fails. I would just eat the handle or in an ideal world catch ALL uhandled exceptions which woud include exceptions that are thrown during a try catch block. – Security Hound Nov 28 '12 at 05:22
6

The best way is to develop your own exceptions for different Layers of application and throw it with inner exception. It will be handled at the next layer of your application. If you think, that you can get a new Exception in the catch block, just re throw this exception without handling.

Let's imagine that you have two layers: Business Logic Layer (BLL) and Data Access Layer (DAL) and in a catch block of DAL you have an exception.

DAL:

try
{
}
catch(Excepetion ex)
{
  // if you don't know how should you handle this exception 
  // you should throw your own exception and include ex like inner exception.
   throw new MyDALException(ex);
}

BLL:

try
{
  // trying to use DAL
}
catch(MyDALException ex)
{
  // handling
}
catch(Exception ex)
{
   throw new MyBLLException(ex);
}
Warlock
  • 7,321
  • 10
  • 55
  • 75
5
try
{
    // Some code here
}
catch (Exception ex)
{
    try
    {
        // Some more code
    }
    catch (Exception ex)
    {
    }
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
2

For the lines of code that could throw an exception in catch block make extra explicit try..ctach block. Besides consider having finally block, to have lines to run by all means there. The same question may raise for the finally block. So if your code is likely to throw some exception in the finally block, you could also add try..catch there.

try
{
}
catch (Exception ex)
{
    try
    {
        // code that is supposed to throw an exception
    }
    catch (Exception ex1)
    {
    }
    // code that is not supposed to throw an exception       
}
finally
{
    try
    {
        // code that is supposed to throw an exception
    }
    catch (Exception ex1)
    {
    }
    // code that is not supposed to throw an exception       
}
horgh
  • 17,918
  • 22
  • 68
  • 123
2

Double-faulting often happens in well-designed 3g programming languages. Since protected mode and the 286, the general design for hardware languages is to reset the chip on a triple fault.

You are probably ok designing your way out of a double fault. Don't feel bad about having to do something to stop processing / report an error to the user in this case. If you run into a case where, eg., you catch an IO exception (reading/writing data) and then try to close the stream you're reading from, and that also fails, its not a bad pattern to fail dramatically and warn the user that something truly exceptional happened.

Pedantic
  • 5,032
  • 2
  • 24
  • 37
1

A catch block isn't special in any particular way. You will have to either use another try/catch block or not handle the error.

Michael Dunlap
  • 4,300
  • 25
  • 36
0

My friend Atul.. if you if write try..catch in catch block, and if again exception occurs in inner try..catch, same problem will raise again. So address this issue you can handle those errors in application level events in Global.asax

check below links..

http://msdn.microsoft.com/en-us/library/24395wz3%28v=vs.100%29.aspx

http://msdn.microsoft.com/en-us/library/fwzzh56s%28v=vs.80%29.aspx

let me know if this works for you.. :)

Anup
  • 80
  • 1
  • 1
  • 6