My Notes on Exception Handling

First, I want to thank my good friend Chew Min for introducing the book, “C# 6.0 and the .NET 4.6 Framework” by Andrew Troelsen and Philip Japikse to me. It is a very good book which includes basics of C# and .NET framework every developer should know. I am going to share my notes on the chapter, “Understanding Structured Exception Handling”.

  • There are mainly 3 types of errors which can be included in a program
    • Bugs – errors made by the programmer
    • User Errors – Errors in user inputs
    • Exceptions – Runtime errors
  • Catch all possible exceptions in production level projects
  • You can throw and handle exceptions, when you know about certain possible occurrence of errors. The keywords used for this are try, catch, throw, finally and when (C# 6.0)
  • Exception class implements
    • _Exception – .NET exception to be processed by an unmanaged code base
    • ISerializable – Exception object to be persisted across boundaries
  • Properties in  Exception class
    • Data – IDictionary type, programmer defined info about the exception (key-value pair)
    • HelpLink – Url to a help page or a document
    • InnerException – information about previous exceptions
    • Message – error message
    • Source – gets/sets name of the assembly
    • Stacktrace – sequence of calls triggered exception, this is useful for debugging to dump the error to external log
    • TargetSite – return MethodBase object, describe information about the method that threw the exception (ToString() will give this)
  • Throwing a new exception
    • throw new Exception(“message”);
  • You are not required to handle each and every exception thrown by the program
  • Some ways to deal with an exception
    • Log information to a report or a file
    • write data to the windows event log
    • email a system administrator
    • display problem to an end user
    • dump content to the console window
  • Exceptions from .NET framework or CLR known as System Exceptions
  • Writing a Custom Exception
    • Derives from Exception/ ApplicationException
    • Marked with the [System.Serializable] attribute
    • Defines a default constructor
    • Defines a constructor, sets the inherited Message property
    • Defines a constructor to handle InnerExceptions
    • Defines a constructor to handle the serialization of your type.
  • Catch most specific exceptions first and then catch the generic exceptions
  • Throw in catch to re-throw exceptions(partial handling)
  • Throw exceptions only until the callers who has the ability to handle incoming exceptions gracefully
  • C# 6.0 has when keyword to filter exception logic
    • eg:  catch(Exception ex) when(isItTrue == True) { // do something }
Advertisements

Author: Harshani Nawarathna

Sri Lankan woman in Computing

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s