When writing a C# application whose #1 priority is to never crash, how often should I used a try-catch block?
Can I encapsulate all the statements in a method in try-catch blocks?
public void SomeMethod() { try { // entire contents of the function // library calls // function calls // variable initialization .. etc } catch (Exception e) { // recover } }
What are the downsides to wrapping everything in try-catch blocks?
The only down side is when an exception is actually thrown. There is no overhead for wrapping the code, except for when exceptions occur.
Also, you don’t want to use try/catch for control flow. Consider this (bad code):
You’ll get more performance from some thing like File.Exists. such as:
EDIT: found the MSDN direct quote: