Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
How do you handle the exception through Asp.net MVC?
Posted

 
Share this answer
 
ASP.NET is just the framework for building applications, MVC can be taken as a flavor for the framework. If you actually, open a project that runs on ASP.NET, you will see that it is written in C# (or VB.NET; both being .NET languages). So the errors that you handle, are handled in the programming language itself.

C# handles the problems like this,

C#
try {
   // Code here
} catch (Exception e) {
   // Handle the error here.
}


Now, that was the method to handle the exception by an idiot programmer (IMO). An expert ASP.NET programmer would try to write the program in such as way, that exceptions are not raised. Most obvious exception is, System.NullReferenceException. He would try to check if the value is null, like this,

C#
if(obj != null) {
   // Code here
}


This would also handle the exception, in a much better way. Because it would allow him to stop the application in a much user-friendly way.

Another way is to handle the errors using custom error page. You can send the user to another page if there is an error in your application. That goes right in the Web.config file and the code is like this,

HTML
<configuration>
  <appsettings />
  <connectionstrings />
  <system.web>
    <compilation debug="true" />

    <!-- Turn on Custom Errors -->
    <customerrors mode="On">
      defaultRedirect="ErrorPage.aspx">
      <error statuscode="404" redirect="Error404.aspx" />
    </customerrors>

  </system.web>
</configuration>


This would handle the error and will redirect, but you will lose why was the exception raised. So I would recommend that you do not use this method, instead handle the exception programmatically and see what could have been done to fix the problem.

References:

https://msdn.microsoft.com/en-us/library/bb397417.aspx
http://www.codeproject.com/Articles/10593/Error-Handling-in-ASP-NET
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900