Errors interception and rising
Description
We will speak here about the errors raised by external conditions (bugs) and the errors rose by the application.
There is not application without bugs. The bugs are not desired for an application – but programmers are human beings and, by consequence, they cannot verify every possible path that go an application to the failure. Maybe the space on hard is too little, other time the network between database and application is broken – there are many conditions to bring failure to an application. And you have to manage priority between
· repairing bugs
· evolving the application
· Resolving failure cases.
The errors raised by the application are usually logical errors, like the fact that a birthday date for a person cannot be tomorrow. Sometimes you will intercept errors from other components and raise your own error (like the fact that a component should have a unique name – you will intercept the index failure raised by your database and then raise your own error).
How to intercept error in applications
We will discuss here how and where to intercept errors. There are two principles that I guide my interception of errors:
1. Occam’s razor : in components(business layer, repository) intercept only the errors that you are sure to know what to do with (such as an ID is missing from a database). Usually here you can raise your own exception and let the GUI handle it. In
2. In GUI layer intercept all exception and , if possible, suggest to the user an alternative way to do.You must also log the error in order to can be retrieved later.
Please read the logging and instrumentation to see a common way to describe the path that a error has been flow through the application
Examples
First example is how to intercept an error
Second will be how to intercept and raise own errors
Third will be about intercepting errors in a Console application
1)We will first intercept an error and write a custom message for this error .
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace exceptionintercept
{
class Program
{
static void Main(string[] args)
{
using (SqlConnection sc = new SqlConnection())
{
sc.ConnectionString = "Data Source=(local);Initial Catalog=adaad!@#$%^;Trusted_Connection=true";
try
{
sc.Open();
}
catch (SqlException ex)
{
if (ex.Number == 2)
{
Console.WriteLine("no database connection : “+ ex.Message);
return;
}
throw;
}
}
}
}
}
As you see we will intercept only the SqlException and put the custom error message only if error message number for the SqlException is 2 – no network access. Usually it is not a good idea to show your connection string in the output shown to the user.
Download project from http://msprogrammer.serviciipeweb.ro/wp-content/uploads/exception.zip
2) Intercept and raise error
We will do now a more advanced interception of connection : we will see if the machine that has the database answers to the ping. If yes, probably only the database is down , not the PC or the connection.
public void VerifyConnection(string MyConnection)
{
using (SqlConnection sc = new SqlConnection())
{
sc.ConnectionString = MyConnection;
try
{
sc.Open();
}
catch (SqlException ex)
{
//TODO : log the exception
Ping p = new Ping();
try
{
PingReply pr = p.Send(sc.DataSource);
}
catch (PingException)
{
//TODO : log the exception
throw new PCException(sc.DataSource, ex);
}
throw new BasicDatabaseException(sc.DataSource,ex);
}
}
}
3) Any application should intercept globally errors, in order to log / save state if there is something that works not well and it is not handled.
static void Main(string[] args)
{
System.AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
int i = 1;
i = 1 / (i - 1);
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
//TODO : log
Console.WriteLine("from program : unexpected error occured " + e.ExceptionObject);
}
Download project from http://msprogrammer.serviciipeweb.ro/wp-content/uploads/exception.zip
TODO
intercept global errors in WinForms, ASP.NET , WPF, Silverlight application
Other resources
ELMAH – intercept ASP.NET errors , http://code.google.com/p/elmah/
Logging : http://msprogrammer.serviciipeweb.ro/2010/04/19/logging-and-instrumentation/