Handling errors in Asp.net application

Until yesterday what I thought about handling exception was just using try catch (that too a general catch) wherever possible and that’s it. Done! When my lead asked me if some one tries to transfer fund from his account to his clients and mean while system threw some exception. But since you have used general exception and did not handle it. How would it look like? The user will assume that money has got transferred, the client will keep waiting for the money to get deposited, and you the developer never knew that an error has happened inside nor you can trace it! Surely it will be disastrous! Now how to go about solving this problem?

  1. We should never a general exception handling any where. If some error happens, let it happen. So that you know that something has gone wrong. Hiding such bug is not going to help any one.
  2. Catch only expected bugs. Use the exact exception class to handle the error you expect. 

Earlier some one has misguided me that try catch is used to hide occurring bugs from the user. But today I realize that try catch is actually implemented to let the responsible person know that some thing wrong has happened. But yes off course using try catch we should be able to prevent the application from getting crashed. How do we let the responsible people know about this issue, specifically if the error happened to occur in database layer or business layer?

The methods written inside the database layer and business layer will be used as sub methods to events and methods written in UI layer. So the error occurred in DB layer and BL layer need to be communicated to UI layer using ‘throw exception’ command unless the responsible guys are never going to know about this. We can have a Custom exception class for implementing this. The custom exception can be created by merely overriding all the methods of System Exception class. Now using the overridden methods we can throw the exceptions from DB layer and BL layer, after that we can catch the exception using custom exception class itself from UI layer. Now we can let the responsible know about the issue by redirecting the application to an error page and display the message there or using a message box.






No comments:

Post a Comment