TERRYSMITH.NET    Doing Objects in VB.NET and C# - EXCEPTIONS  
             Home              Painting              Photography              Software              Writing
Previous ] [ Index ]

7

Exceptions

Now that you have a big inheritance, you're ready to get wild and start throwing exceptions! VB.NET and C# both support the standard Try...Catch...Finally blocks, so let's not bore ourselves with that. Instead, let's briefly focus on defining our own, personal, one-of-a-kind, custom exceptions. If you have a Porsche or Ferrari in L.A. or Miami you're "normal" (and boring). Only the real jerks have custom-built hot rods. The same applies in Nerdville with regards to exceptions. Only the real nerds have custom-built, hot rod exceptions.

Custom exceptions are generally derived from System.ApplicationException. They can implement custom constructors but should typically initialize the Message property of their base class. Below is an example in C# followed by a simple harness for demonstrating it's use. Note how the constructor for the NuclearMealtdownException initializes the base class:

public class NuclearMealtdownException : System.ApplicationException
{
   private double mGammaRaysPerSecond = 0.0;

   public NuclearMealtdownException(string message, double gammaRaysPerSecond) : base(message)
   {
      mGammaRaysPerSecond = gammaRaysPerSecond;
   }

   public double GammaRaysPerSecond
   {
      get
      {
         return mGammaRaysPerSecond;
      }
   }

   public override string ToString()
   {
      return base.ToString() +
             "\n\tGammaRaysPerSecond: " +
             mGammaRaysPerSecond.ToString();
   }
}

public class ExceptionDemo
{
   public static void Main()
   {
      try
      {
         NuclearMealtdownException ohNo = new NuclearMealtdownException(
            "Run like hell!", double.PositiveInfinity);
         throw ohNo;
      }
      catch ( System.ApplicationException exception )
      {
         // Log it, then throw it away.
         // It's probably not important.
         Trace.WriteLine(exception.ToString());
      }
   }
}

Running this code yields the following minor warning:

CSCustomExceptionExample.NuclearMealtdownException: Run like hell!
   at CSCustomExceptionExample.ExceptionDemo.Main(String[] args) in c:\dotnetdev\cscustominheritanceexample\main.cs:line 14
   GammaRaysPerSecond: Infinity

The NuclearMealtdownException class overrides the ToString method in order to insert the GammaRaysPerSecond attribute into the output. However, the property's value is at the end of the stack trace and, in the event of a real meltdown scenario, would probably be ignored. Alternatives would be to insert the value into the base class's Message property as the class is being constructed or to write a totally custom ToString method and output any custom property values before outputting base.StackTrace.

Here is the equivalent code in VB.NET:

Public Class NuclearMealtdownException
   Inherits System.ApplicationException

   Private mGammaRaysPerSecond As Double

   Public Sub New(ByVal message As String, ByVal gammaRaysPerSecond As Double)
      MyBase.New(message)
      mGammaRaysPerSecond = gammaRaysPerSecond
   End Sub

   Public ReadOnly Property GammaRaysPerSecond() As Double
      Get
         Return mGammaRaysPerSecond
      End Get
   End Property

   Public Overrides Function ToString() As String
      Return MyBase.ToString() + _
             vbCrLf + vbTab + _
             "GammaRaysPerSecond: " + _
             mGammaRaysPerSecond.ToString()
   End Function
End Class

Module Main
   Public Sub Main()
      Try
         Dim ohNo As New NuclearMealtdownException("Run like hell!", Double.PositiveInfinity)
         Throw ohNo
      Catch exception As NuclearMealtdownException
         'Log it, then throw it away.
         'It's probably not important.
         Trace.WriteLine(exception.ToString())
      End Try
   End Sub
End Module

The result melts down to this:

VBCustomExceptionExample.NuclearMealtdownException: Run like hell!
   at VBCustomExceptionExample.Main.Main() in C:\DotNetDev\VBCustomExceptionExample\Main.vb:line 5
   GammaRaysPerSecond: Infinity
Previous ] [ Index ]

Amazon Honor System Click Here to Pay Learn More


All images and text on this site are licensed only for viewing on your computer during your visit. No rights to save, copy, print, redistribute, use in derivative works, or in any other manner are allowed or implied without the prior written consent of the author.

All images and text are ©Terry Smith unless otherwise noted. All rights reserved.

terry@terrysmith.net