Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an asp.net c# application, running pretty good in production. It's a vast project and almost every method has try catch block with no exception handling(no code in catch), now if any error occurs instead of debugging all code and find exception, I want to handle it globally. Any suggestions?
Posted
Comments
Prasad Khandekar 7-Jul-15 7:49am    
Hello,

You can have a look at PostSharp Aspect Framework. (https://www.postsharp.net/diagnostics/net-logging)

Regards,
Prasad P. Khandekar

As you already stated - most of your try-catch blocks have no any exception handling, so the only thing they do is hiding any error (exception) you have in your code...
Do not use try-catch without good reason!!! Never!!! Try-catch is very expensive and for that it intended for exceptional errors only...Do not use it as a solution for input validation for instance or to overcome problems of missing files and so on...

If you want to handle all unhandledexceptions in one place utilize the Application_Error method in Global.asax...
https://msdn.microsoft.com/en-us/library/24395wz3(v=vs.140).aspx[^]
 
Share this answer
 
Comments
Kornfeld Eliyahu Peter 7-Jul-15 8:06am    
It is not just a solution, but also points out how it is wrong to add try-catch everywhere!!!
Add try-catch to only the few really needs it and get all the other - unexpected - errors in one place (and not by calling a method from hundred of places...)
Dave Kreskowiak 7-Jul-15 8:09am    
Actually, YOU need to go re-read the question. He already has try/catch blocks all over the place and they do nothing but very bad things.

A "global" error handler really isn't a solution here because all of the exceptions are already being "handled" (swallowed) so there's close to no exceptions that the global handler will ever see.

If he has try/catch blocks in every method, he has to pretty much rewrite all of his code to get out the problem he created.
Kornfeld Eliyahu Peter 7-Jul-15 8:10am    
Me think OP has to re-write all his application in every case - and if so it is better to do it in the right way...
AnvisNet 7-Jul-15 8:52am    
Exactly, I am aware of try catch block and when to use, but code has been already developed by some one and i am dealing with it. Looking for quick resolution here..,,
Thanks for suggestions
Kornfeld Eliyahu Peter 7-Jul-15 8:56am    
If this is a closed code - means you can not remove the try-catch block, than you doomed!!!
As you may already understood it those - empty - try-catch block do nothing but swallowing the exceptions, so you can not can get them nowhere else...
I think you need to read a couple articles. this[^], this[^] and this[^]
 
Share this answer
 
Comments
AnvisNet 7-Jul-15 8:56am    
Thanks Dave, vl check the links provided and get back.,

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