When customize an exception handling. First thing we want to maintain is the exception message as it should understandable to the layman. For this remove the technical terms from the exception messages as much as possible and just display the message content and required information only in such a way that user thing it's nothing, but can fix very soon. Otherwise imagine a situation like displaying a huge system generated sqlexception message and user is so upset of seeing this and developer wants a complete search through the code to fix it.
For our customized message to store a resource file needed. Just create a new C# web application and add a resource file (Resource1) to it. Also add 2 classes with names MyException&Operations. MyException is our customized class to handle exception which extends "ApplicationException" system class and Operations, just like the name referred, has 2 function Divide() & AssignArray(). Both these functions will generate 2 exceptions and we are going to handle it. From the web form we will call these 2 functions. One thing I want to say is that I am not considering the true layer structure, as that's not the primary aim of this article. You also wants to understand that in a well structured design web form has no direct access to Operations class. But it will access through a business layer. Ok let come back to our subject. Add the following 2 name value collections to the resource file.
- DivideByZeroException - DivideByZeroException occurred. Function-{0}, Class-{1}
- IndexOutOfRangeException - IndexOutOfRangeException occurred. Function-{0}, Class-{1}
So now our resource file has 2 rows with 2 columns each and will be used in our exception message display. Both Values in the resource file have 2 arguments. Among them {0} position will occupy by function name and {1} position will occupy by class name. You can access the resource file by the following way
ResourceManager rm = new ResourceManager ("WebApplication1.Resource1", Assembly.GetExecutingAssembly ());
You also want to add the name spaces System. Resources and System. Reflection. You can access a value from resource file by
< ResourceManager object>.GetString ("<Corresponding name in the resource file>")
In the web form we will handle the exception using MyException class like follows
catch (MyException exception)
{
Label1.Visible = true;
Label1.Text = exception.Message;
}
It actually from in side the Operations class, the MyException class is throwing and set with present exception values as follows
throw new MyException (string.Format (<Value from the resource file>),"<function name>"," <Class name>"), <Occurred exception>);
So ultimately the message contains the message we defined in the resource file which includes the error function name and class name. One good practice want to keep is that you should always catch the specific exception inside the Operations class (i.e. from where custom exception class throws). So don't use general "Exception" Type but use specific exceptions like DivideByZeroException etc... But in every specific exception case we will throw our own MyException class like below case
catch (DivideByZeroException exception)
{
throw new MyException (string.Format (rm.GetString ("DivideByZeroException"),"Divide","Operations"),
exception.InnerException);
}
Please use the code along with this to get a better idea and send your responses also.