dbdp
asked on
Use of Try/Catch with c# 2010
I am using c# 2010 and have religiously put a catch for all possible exceptions in a method, followed by the most generic (Exception Ex)
Is this really necessary, if I just use the generic cathall it still seems to give me correct error so should I continue using all possible exceptions or not?
Any advice?
Is this really necessary, if I just use the generic cathall it still seems to give me correct error so should I continue using all possible exceptions or not?
Any advice?
Generally (pun intended) the general exception is all you need. If you know you might be hitting a specific exception and want to get that specific info, then catch the specific exceptions.
you can use the cathall normally, but if you want to hadle a specific exception then catch first the specific exeption.
Case: supose that you have a block of and inside you work with a file and work with database, then if you want to handle the file error then catch the fileexception before the gereric catchall
Case: supose that you have a block of and inside you work with a file and work with database, then if you want to handle the file error then catch the fileexception before the gereric catchall
The answer (as with so many programming tasks) is "it depends". If you simply want to trap exceptions and either log/report them to the user then you can just catch the generic Exception. If it's something that needs special handling (such as an operation you can re-try if it fails, or switch to a failover server, etc) then you will want to catch a more specific exception type.
i would prefer to use the exceptions and give a user useful interpretation what can be done to rectify the issue using the different type of exceptions. After that i will write a generic exception and log it as unhandled exception. just giving an example if i am using a webservice then may be the server is down or some network is busy, then i can show the user that try after some time when server is up or if the network bandwidth issue then ask him to do the heavy processing when the load on the server is less etc.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Very detailed yet easy explanation!