Spedward
Spedward

Reputation: 179

What happen if the code within a catch fails

Using c# code within a web application, I have a try catch statement in my code and within the catch I want to do several things such as email an administrator.

I am wondering what would happen if the email code or other code within the catch fails? Is there a way to handle a general exception in the application?

Upvotes: 2

Views: 1369

Answers (9)

Guillaume Slashy
Guillaume Slashy

Reputation: 3624

There is no incompatibily with the fact to use a try catch sequence in a catch snippet Logically, you won't catch the same exception so you can know what part failed

Lot of guys gave u the code to use, accept one of these answers

Upvotes: 0

Nighil
Nighil

Reputation: 4127

this is for a windows application

 static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                AppDomain.CurrentDomain.UnhandledException += AppDomain_UnhandledException;

                Application.ThreadException += Application_ThreadException;
                Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

            }
            static void Application_ThreadException(Object sender, ThreadExceptionEventArgs e)
            {
                MessageBox.Show(e.Exception.Message, "Application.ThreadException");
            }

            static void AppDomain_UnhandledException(Object sender, UnhandledExceptionEventArgs e)
            {
                MessageBox.Show(((Exception)e.ExceptionObject).Message, "AppDomain.UnhandledException");
            }

Upvotes: 1

Azodious
Azodious

Reputation: 13882

I am wondering what would happen if the email code or other code within the catch fails?

The exception thrown within catch will be handled by CLR Default Handler if not enclosed within another try-catch pair.

However finally will definitely execute. so you can use it to clearing or closing the resources.

Is there a way to handle a general exception in the application?

within main method, write following code part:

try
{
    // write normal code
}
catch(Exception e)
{
    // if here exception occurs ... God Help.
}

Upvotes: 1

dknaack
dknaack

Reputation: 60516

Description

  1. There are 2 things you can do to catch unhandled exceptions. Application.ThreadException and AppDomain.CurrentDomain.UnhandledException

    Application.ThreadException Occurs when an untrapped thread exception is thrown.

    AppDomain.CurrentDomain.UnhandledException Occurs when an exception is not caught.

  2. Handle a exception inside the catch block.

Sample

  1. Application.ThreadException and AppDomain.CurrentDomain.UnhandledException

    Application.ThreadException +=
        new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
    
    AppDomain.CurrentDomain.UnhandledException += 
         new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
    
  2. Handle a exception inside the catch block.

    try
    {
        // do something
    } 
    catch (Exception ex)
    {
        try
        {
            // send E-Mail
        } 
        catch
        {
            // handle 
        }   
    }
    

More Information

Upvotes: 5

Krizz
Krizz

Reputation: 11552

If the exception is thrown in catch block it will be propagated the normal way exceptions are.

You will need the following code:

try 
{
  //code 
}
catch (SomeKindOfError e1)
{
    try 
    {
       // send email
    }
    catch (EmailError e2)
    {

    }
}

Your exception would be caught by any outer block where it is eligible, e.g. if you have:

try 
{
   // ...
   try {
      //code 
   }
   catch (SomeKindOfErrorThatIsNotEmailError e1)
   {
      // send email
   }
   // ...
}
catch 
{
    // your email exception will get caught here
}

Upvotes: 1

Ravi Gadag
Ravi Gadag

Reputation: 15881

you can try aplication exception, if it's fatal error. , though its become obsolete Checkthis now in .net framework.

Application exceptions

public sealed class Product
{
  private String name;
  public String Name
  {
    get { return name; }
    set
    {
      if (value != null) name = value;
      else throw new UndefinedNameException();
    }
  }
  public sealed class UndefinedNameException : ApplicationException
  {
    public UndefinedNameException() : base("Name cannot be null") {}
  }
} // end of class Product

Upvotes: 1

CloudyMarble
CloudyMarble

Reputation: 37576

Sure, you will have to surround the routine where you have your try catch with another try catch, or write your code in the catch statement surrounded with a try catch statement.

Upvotes: 1

ChrisF
ChrisF

Reputation: 137178

One approach is to handle the exception within the catch statement:

try
{
    // Do stuff
}
catch (SpecificException ex)
{
    try
    {
        // Try e-mailing
    }
    catch (AnotherException ex1)
    {
        // Write local log file
    }
 }

this will allow the program to continue.

However, if the program can't you can handle all unhandled exceptions at the application level and do something there.

Upvotes: 1

Shawabawa
Shawabawa

Reputation: 2597

If an exception is thrown within the catch clause, it simply raises another exception. One way to solve it is to simply have a nested try, eg.

try {
  <stuff>
} catch(Exception e) {
  try {
    <email admin>
  catch(EmailException e2) {
    <stuff>
  }
  <stuff>
}

Upvotes: 1

Related Questions