In any software application, you need to handle error with care.To do so, in asp.net mvc they have an attribute called [HandleError] which will raise when any error happens in the software application.The errors may occur during compile time and run time.The errors can occur due to invalid syntax,invalid data,invalid database communication etc.
The handleerror attribute has following properies.
How to use the properties in handleerror attribute?
Syntax:
[HandleError(Order = 1, View = "Error", ExceptionType = typeof(IndexOutofRangeException))]
If any error occurs during action execution, it will find view named Error in views folder and render that page to the user.
To set the custom error for your web application you need to first enable customerrors in web.config like this
<system.web>
<customErrors mode="On"><customErrors>
<system.web>
Ok , come let us jump into the demo project of how to handle errors.
Step 1:
Create a new ASP.NET MVC project:
Step 2:
Select empty MVC template and click OK.
Step 3:
Right click on controller folder and add empty controller
Step 4:
Homecontroller.cs
public ActionResult Index()
{
int i = 100;
ViewBag.ivar = i;
int j = i / 0;
ViewBag.jvar = j;
return View(j);
}
Step 5:
Homecontroller View code:-
I value: @ViewBag.ivar
J Value : @ViewBag.jvar
Step 6:
If you run the above code , you will get the following exception
Step 7 :
How to avoid the above error?
It is very simple, just include [Handlerror] attribute above the Index().
[HandleError]
public ActionResult Index()
{
int i = 100;
ViewBag.ivar = i;
int j = i / 0;
ViewBag.jvar = j;
return View(j);
}
Step 8:
Now add error.cshtml inside Shared folder.
Step 9:
Include customerrors mode=on in web.config.
Step 10:
Now run the project, you will see compile time error ,because you are attempting to divide large number by zero.Just hit Run.
Step 11:
Now you can view your custom errors using Error.cshtml like this,
Step 12:
you can create view for each specific error type, for eg:- if you got dividebyzero exception then you can show it in seperate view called dividebyzeroerror.cshtml.
suppose if you got "Index out of bound Exception: Accessing array of an element which is not present" then you can show it in seperate view called indexout.cshtml
Step 13:
I am going to create dividebyzeroerror.cshtml file inorder to show my dividebyzero exception.
Step 14:
Right click Shared folder and create a view called "dividebyzeroerror" and click Ok.
Step 15:
Now in HomeController, Modify the code like this,
[HandleError(View="dividebyzeroerror")]
public ActionResult Index()
{
int i = 100;
ViewBag.ivar = i;
int j = i / 0;
ViewBag.jvar = j;
return View(j);
}
Step 16:Execute the project and see the result
Step 17:
Handling more than 1 exception type at a time with HandleError attribute:-
Now i try to add 2 different exception one is divide by zero exception and other is index out of range. Let us see how to do it?
[HandleError(Order = 2,View="dividebyzeroerror", ExceptionType = typeof(DivideByZeroException))]
[HandleError(Order = 1, View = "IndexOutofRangeEerror", ExceptionType = typeof(IndexOutOfRangeException))]
public ActionResult Index()
{
try
{
//INDEX OUT OF RANGE EXCEPTION
int[] a = new int[3] { 1, 2, 3 };
ViewBag.aarrayvar1 = a[0];
ViewBag.aarrayvar2 = a[1];
ViewBag.aarrayvar3 = a[3];
//DIVIDE BY ZERO EXCEPTION
int i = 100;
ViewBag.ivar = i;
int j = i / 0;
ViewBag.jvar = j;
}
catch(DivideByZeroException ex)
{
throw ex;
}
catch(IndexOutOfRangeException ex1)
{
throw ex1;
}
return View();
}
Step 18: Run the project and see , You will get index out of range exception,