Reputation: 641
I want to get all the ip addresses of my computer. If something goes wrong (exception), I simply want empty string returned. Here is the function I use. GetHostEntry
will throw several exceptions, and GetHostName
also throws exception. How should I handle all these exceptions? Should I catch each of them one by one? That will make code cluttered. Or Should I simply use catch (Exception e)
and do nothing inside the catch
block? What is the best way to handle it?
private string GetIpAddress()
{
var temp = new StringBuilder();
try {
var hostEntry = Dns.GetHostEntry(Dns.GetHostName());
var ips = from address in hostEntry.AddressList
where (address.AddressFamily == AddressFamily.InterNetwork)
select address;
foreach (IPAddress ip in ips) {
temp.Append(ip).Append(" ");
}
} catch (exception1) {
} catch (exception2) {
} .....
return temp.ToString();
}
Upvotes: 1
Views: 133
Reputation: 11040
Do not mute exceptions, at the very least write a log line. Even if you assume it's going to be a known kind of exception for a known reason.
Don't catch an exception for no apparent reason - let it bubble up
Do put try/catch on every external entry point - event handlers, threads etc. Exception that bubbles up from a button click handler will cause your app to crash, same goes for code on another thread or even on the same thread if forced to run on it (such as Windows.Forms.Control.Invoke(delegate))
Have the least amount of different handlers and only if you're actually treating the exceptions differently
Add a listener to AppDomain.UnhandledException and log these too
Upvotes: 0
Reputation: 1700
Handle the ones you can do something about, or that you want to handle in a specific way ( think failed to connect driving a message box to prompt whether or not youre connected to the network)
For other "its gone wrong" exceptions, let them propagate up to where we it is meaningful to handle it, or rethrow a meaningful, contextual exception and handle where it is appropriate.
If all youre tryng to do is send a report over a network of some process, to a log file, does it make sense to let that exception kill your process? Not really, so just wrap the top level call to SendNetworkReport.
If its central to your whole process, then let it propagate right up to your main control code, and abort the process in some contextually signifcant way.
Upvotes: 1
Reputation: 9314
As a rule of thumb, if you can't handle the exceptions, as evidenced when you have an empty catch block, then you should let them bubble up to the next level.
You should look at each exception that can be thrown and determine exactly why it could be thrown and what you should do about it. For example, if DnsGetHostEntry() can throw an exception, why would it? Should you return a host not found error? Is there a sane default that you should return, that makes sense in your application?
Upvotes: 1
Reputation: 13706
If you really want to just throw away the exception, use an empty catch
.
try
{
// Code
}
catch {}
(Note that that's a pair of curly braces, not ()
's)
Upvotes: 1
Reputation: 150108
You should catch a particular Exception if and only if you can do something useful about it. Otherwise, let the Exception propagate to a level that can do something useful with it.
You should have a global exception handler that elegantly manages otherwise-uncaught exceptions gracefully.
Upvotes: 4