What is the best practice for exception management and error handling c#
-
whenever i wrote code and if i smell that any error or exception may occur at run time then i use those code in try {} catch {} block and show exception related message from catch block but i like to what is the best industry standard for exception management and error handling technique? i like to show a friendly message to user as a result any lay man could understand what problem is occurring. guide me with best practice for exception management and error handling. thanks
tbhattacharjee
-
whenever i wrote code and if i smell that any error or exception may occur at run time then i use those code in try {} catch {} block and show exception related message from catch block but i like to what is the best industry standard for exception management and error handling technique? i like to show a friendly message to user as a result any lay man could understand what problem is occurring. guide me with best practice for exception management and error handling. thanks
tbhattacharjee
You are not going to learn how to be a good programmer just by posting questions here. You need to go and do some studying, and then practice what you have learned. I already gave you a link to Charles Petzolds's book, which will give you a good start on the basics of .NET and C#; you really should read it. You can then follow that with some more advanced books which you can discover through Google.
-
whenever i wrote code and if i smell that any error or exception may occur at run time then i use those code in try {} catch {} block and show exception related message from catch block but i like to what is the best industry standard for exception management and error handling technique? i like to show a friendly message to user as a result any lay man could understand what problem is occurring. guide me with best practice for exception management and error handling. thanks
tbhattacharjee
The answer to the question is always throw exceptions. The only exception I can think of is some file handling situations where you can get an exception - but as previously mentioned the best thing to do is read up about it and use google. As a general tip you should not handle exceptions as by handling an exception you are running the risk of corrupting any data in your system.
“That which can be asserted without evidence, can be dismissed without evidence.”
― Christopher Hitchens
-
whenever i wrote code and if i smell that any error or exception may occur at run time then i use those code in try {} catch {} block and show exception related message from catch block but i like to what is the best industry standard for exception management and error handling technique? i like to show a friendly message to user as a result any lay man could understand what problem is occurring. guide me with best practice for exception management and error handling. thanks
tbhattacharjee
Tridip Bhattacharjee wrote:
what is the best industry standard for exception management and error handling technique?
It depends on the situation. An exception is supposed to represent an exceptional condition within the API that is being used. The caller is then responsible for determining what that exception means - to the caller. Thus the caller can catch it and do something or not catch it all. That is a general answer but it is further modified by what the caller is supposed to be doing. For example it is inappropriate to allow a SQL syntax error to bubble up to a UI to leave the consumer user to deal with it. But on the other hand one must tell the user that what they were trying to do didn't work. That means somewhere in the middle something must catch that exception, do something with it, and in some way relay a different error message to the user. A common way of dealing with exceptions is to use a logging api and log it. Like everything that requires moderation and thought as well since one should probably also log other, non-exceptional information and also not log every exception because some are in fact expected (thus generally pointless to log.)
-
whenever i wrote code and if i smell that any error or exception may occur at run time then i use those code in try {} catch {} block and show exception related message from catch block but i like to what is the best industry standard for exception management and error handling technique? i like to show a friendly message to user as a result any lay man could understand what problem is occurring. guide me with best practice for exception management and error handling. thanks
tbhattacharjee
Well, not to mention what has already been said, but when I first started learning, I would just download OPEN SOURCE projects i could find on the internet, and just read their code. See how they figured things out and how they handled certain things throughout their application. After doing this to as many as 20 - 30 projects, I really started to get the feel of what is COMMON PLACE and what is not. AS far as your question, i think you answered it yourself:
Tridip Bhattacharjee wrote:
i use those code in try {} catch {} block and show exception related message
As that is what those are meant to be used for. The bigger question is "when should you use them?":confused:
I Fart Ideas every day! It's just only once in a while one does not stink! :)