Exception handling in C #

I know this may be a little strange, but doubt is in doubt ... what will happen in the following situation ...

private void SendMail()
{
    try
    {
        //i try to send a mail and it throws an exception
    }
    catch(Exception ex)
    {
        //so i will handle that exception over here
        //and since an exception occurred while sending a mail
        //i will log an event with the eventlog

        //All i want to know is what if an exception occurs here
        //while writing the error log, how should i handle it??
    }
}

Thank.

+5
source share
8 answers

I personally would have wrapped a call to write to the event log using another try \ catch statement.

However, ultimately, it depends on your specification. If it is extremely important for the system that the error is recorded in the event log, you must allow it to be thrown. However, based on your example, I doubt that this is what you want to do.

+4
source

. , - , .

private void SendMail()
{
    try
    {
        //i try to send a mail and it throws an exception
    }
    catch(Exception ex)
    {
        WriteToLog();
    }
}

private void WriteToLog()
{
    try
    {
        // Write to the Log
    }
    catch(Exception ex)
    {
        // Error Will Robinson
        // You should probably make this error catching specialized instead of pokeman error handling
    }
}
+4

try-catch. , .

+1

try-catch catch.

0

(, ...), . , , , ...

/ .

0

. . try-catch catch . . , , , . , , / . catch, , :

  • , .
  • ,

catch API, , . API , ( , , ..). , , .

0

, .

public static class MyExtentions
{
    public static void LogToErrorFile(this Exception exception)
    {
        try
        {
            System.IO.File.AppendAllText(System.IO.Path.Combine(Application.StartupPath, "error_log.txt"),
                String.Format("{0}\tProgram Error: {1}\n", DateTime.Now, exception.ToString()));
        }
        catch 
        { 
            // Handle however you wish
        }
    }
}

:

try
{
   ...
}
catch(Exception ex)
{
   ex.LogToErrorFile();
}

catch , , . .

0
  • -, , "" catch. , (SmtpException), - ( ). - . , , / .

  • , , , Application_Error . .

  • , , , , .

  • ASP.NET Peter A. Bromberg Peter A. Bromberg -

  • One more thing, if your application makes a mistake / throws an error that cannot cope (in general), it is better that it goes down gracefully and does not continue. Unstable use is not a good idea.

0
source

All Articles