Exception Handling

Home DM Courses Fun Additional Resources Exercises On-Sites

        Structured exception handing is your friend!

1. Use try / finally try { ... } finally { ... }
2. Catch and handle
expected exceptions
try { ... } catch(DivideByZeroException) { ... }
3. Do not catch all exceptions
    - unhandled exceptions
      should kill your app
     
(after cleanup)
try { ... } catch { ...}

try { ... } catch (Exception ex) { ...}

Except: Win Form or WPF apps (handle Application event)

Be aware of exceptions on other threads (delegate EndInvoke)

4. Don't be afraid to throw exceptions throw new InvalidOperationException()...
5. Use code snippet to create custom exceptions marked as [Serializable], has needed overloaded ctors
6. Wrap exceptions in bool method if performance critical For ex, TryParse, TryGetValue, etc