Exception handling in C#

Exception Handling in C#. Most .NET languages support exception handling. Essentially, when an error occurs in the application, the .NET Framework throws an exception that represents the problem.

You can catch this object using an exception handler. If you fail to catch exception, code will be aborted, and the user will see an error message on page.
Visual Studio provides a useful tool to browse through the exceptions in the .NET class library. Simply select Debug ? Exceptions from the menu

Exception Handling with Try  and Catch

protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            decimal a, b, result;
            a = Decimal.Parse(txtNum1.Text); 
            b = Decimal.Parse(txtNum2.Text); 
            result = a / b; 
        }
        catch (Exception err)
        {
            lblResult.Text = “<b>Message:</b> ” + err.Message + “<br /><br />”; 
            lblResult.Text += “<b>Source:</b> ” + err.Source + “<br /><br />”; 
            lblResult.Text += “<b>Stack Trace:</b> ” + err.StackTrace; 
            
                   
        }
    }
Logging Exceptions
In many cases, it’s best not only to detect and catch exceptions but to log them as well. For example, some problems may occur only when your web server is dealing with a particularly large load. Other problems might recur intermittently, with no obvious causes. To diagnose these errors and build a larger picture of site problems, you need to log exceptions so they can be reviewed later.

Writing Event Log
You can write your exception message to Event Log using System.Diagnostics namespace.

Below code to write exception to event log:
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            decimal a, b, result;
            a = Decimal.Parse(txtNum1.Text); 
            b = Decimal.Parse(txtNum2.Text); 
            result = a / b; 
        }
        catch (Exception err)
        {
            lblResult.Text = “<b>Message:</b> ” + err.Message + “<br /><br />”; 
            lblResult.Text += “<b>Source:</b> ” + err.Source + “<br /><br />”; 
            lblResult.Text += “<b>Stack Trace:</b> ” + err.StackTrace; 
            // Write the information to the event log.             
            EventLog log = new EventLog();             
            log.Source = “MyCustomError”;             
            log.WriteEntry(err.Message, EventLogEntryType.Error);         
        }
    }

Instead of writing to Windows event log, create your own custom application log.
Below code will create a application log “SPCafe”

protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            decimal a, b, result;
            a = Decimal.Parse(txtNum1.Text); 
            b = Decimal.Parse(txtNum2.Text); 
            result = a / b; 
        }
        catch (Exception err)
        {
            if (!EventLog.SourceExists(“SPCafe”))
            {
                EventLog.CreateEventSource(“Wrong Input”, “SPCafe”);
            }
            lblResult.Text = “<b>Message:</b> ” + err.Message + “<br /><br />”; 
            lblResult.Text += “<b>Source:</b> ” + err.Source + “<br /><br />”; 
            lblResult.Text += “<b>Stack Trace:</b> ” + err.StackTrace;            
            EventLog log = new EventLog(“SPCafe”);             
            log.Source = “MyCustomError”;             
            log.WriteEntry(err.Message, EventLogEntryType.Error);         
        }
    }

Retrieve all Log using Log name, Log source:

protected void Button2_Click(object sender, EventArgs e)
    {
        lblResult.Text = “”;         
        if (!EventLog.Exists(txtLog.Text))
        {
            lblResult.Text = “The event log ” + txtLog.Text;
            lblResult.Text += ” doesnt exist.”;
        }
        else
        {
            EventLog log = new EventLog(txtLog.Text);
            foreach (EventLogEntry entry in log.Entries)
            {
                if (chkAll.Checked || entry.Source == txtSource.Text)
                {
                    lblResult.Text += “<b>Entry Type:</b> “;
                    lblResult.Text += entry.EntryType.ToString();
                    lblResult.Text += “<br /><b>Message:</b> “;
                    lblResult.Text += entry.Message;
                    lblResult.Text += “<br /><b>Time Generated:</b> “;
                    lblResult.Text += entry.TimeGenerated;
                    lblResult.Text += “<br /><br />”;
                }
            }
        }
    }

Please follow and like us:

Leave a Comment