Search

Categories

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Send mail to the author(s) E-mail

# Friday, 09 January 2015

http://stackoverflow.com/questions/116896/visual-studio-how-to-break-on-handled-exceptions

image

VS2005 – Ctrl Alt E

If you’re inside a try catch this is a way to easily get the debugger to stop on it.

Also good to just have a good logger there too.

| | # 
# Thursday, 16 October 2014

TL;DR

· Only handle exceptions that you can actually do something about!!  eg Always enclose every piece of code that is run externally in a try/catch eg File read, webservice call… so can gracefully show the user

· Let the global exception handler for the application (eg via global.asax in MVC) catch/handle/log/redirect to error page for the rest of the exceptions.

- Explore EMLAH

http://www.hanselman.com/blog/GoodExceptionManagementRulesOfThumbBackToBasicsEdition.aspx

Only handle exceptions that you can actually do something about!!

http://stackoverflow.com/questions/14973642/how-using-try-catch-for-exception-handling-is-best-practice

  • Catch all unhandled at a global level, and maybe show to user? and log to file
  • Always enclose every piece of code that is run externally in a try/catch
  • Enclose ops that I know might not work eg IO, division by 0..

This is very bad: as the original exception is lost

catch (Exception ex) {
   throw ex;
}

This is bad. Exception is lost

catch (Exception ex) {
   // do nothing ie swallowing exception
}

This is good. Although code may be redundant (unless you need a Finally?)

catch (Exception ex) {
   throw;
}

This is good as the SomeException will have an inner Exception containing the original exception

catch (Exception ex) {
   throw new SomeException(“Some detailed message”, ex);
}

Throwing

Domain/Business logic

class Program { static void Main() { var person = new Person { Name = "" }; Console.WriteLine(person.Name); Console.ReadLine(); } } public class Person { // Backing field (private) private string name; // Property - used to expose field // Properties allow validation on incoming values public string Name { get { return name; } set { if (String.IsNullOrEmpty(value)) { throw new ArgumentException("Name cannot be null or empty"); } name = value; } } }

Simple throwing an exception

Handling

Inner exceptions:

private static void Main() { try { ReadFromFile(); } catch (Exception ex) { throw new Exception("main", ex); } } private static void ReadFromFile() { string[] lines = File.ReadAllLines("grades.txtx"); foreach (string line in lines) { float grade = float.Parse(line); Console.WriteLine(grade); } }

image

throw new Exception(“main”) is bad – lose the inner!

Custom Exceptions

private static void Main() { ReadFromFile(); } private static void ReadFromFile() { try { string[] lines = File.ReadAllLines("grades.txtx"); foreach (string line in lines) { float grade = float.Parse(line); Console.WriteLine(grade); } } catch (Exception ex) { throw new GradesFileNotFoundException("File not found in the directory", ex); } } // Applicaiton exception is thrown by user program... bad now... public class GradesFileNotFoundException : Exception { public GradesFileNotFoundException() { } public GradesFileNotFoundException(string message) : base(message) { } public GradesFileNotFoundException(string message, Exception inner) : base(message, inner) { } }

**but when are these actually useful?

 

Global Application Handling

private static void Main() { // Global exception handler for a ConsoleApp AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionTrapper; ReadFromFile(); } static void UnhandledExceptionTrapper(object sender, UnhandledExceptionEventArgs e) { Console.WriteLine(e.ExceptionObject.ToString()); Console.WriteLine(); Console.WriteLine("Bad things have happened - we've logged the error and sent to devs... Press Enter to continue"); Console.ReadLine(); Environment.Exit(1); } private static void ReadFromFile() { try { string[] lines = File.ReadAllLines("grades.txtx"); foreach (string line in lines) { float grade = float.Parse(line); Console.WriteLine(grade); } } catch (Exception ex) { throw new GradesFileNotFoundException("File not found in the directory", ex); } } // Applicaiton exception is thrown by user program... bad now... public class GradesFileNotFoundException : Exception { public GradesFileNotFoundException() { } public GradesFileNotFoundException(string message) : base(message) { } public GradesFileNotFoundException(string message, Exception inner) : base(message, inner) { } }
http://codebetter.com/karlseguin/2008/05/30/foundations-of-programming-pt-8-back-to-basics-exceptions/
  • Handling
  • Creating
  • Throwing

Handling

Catching exceptions and not actually handling them – Exception swallowing

Creating

using (SqlConnection connection = new SqlConnection(FROM_CONFIGURATION)) using (SqlCommand command = new SqlCommand(“SomeSQL”, connection)){ connection.Open(); command.ExecuteNonQuery(); }

If ExecuteNonQuery throws an exception, this will dispose of the connection.
  • Don't swallow exceptions
  • Don’t catch exceptions unless you can handle them
  • Make use of custom exceptions
| | #