welcome to my blog
The best place on the internet for random code!
www.therealgromer.com









Apr
27
    
C# Custom Exceptions Made Easy!
Posted (Mike) on 27-04-2007

If you are a programmer, I’m sure you have used try/catch blocks in your code to hopefully eliminate any dreadful unhandled exceptions. We all know they usually happen only when giving a presentation and that’s no good! You could go by throwing generic exceptions all day long…

  1. throw new Exception(“Hey, you broke my program :(”);

That’s a bit generic though. This is where custom exceptions come in. It’s more intuitive and much less generic than throwing Exception() every time. It will also give you better control over choosing what to do when any exception is thrown. Instead of having one catch block, you’ll have the catch blocks for your custom exceptions with the generic Exception() last to catch anything that got past.

First thing you’ll need to do is create a Windows Library project. I named mine CustomExceptions.

Create project window.

When the new project is open, you’ll have a file called Class1.cs which you can rename to the name of the exception you are going to create. When you do this, you will see a window like this:

Rename file popup.

Just click the Yes button and your class name will change throughout your project. Now we’re ready to do the actual coding. When creating custom exceptions, we must extend the ApplicationException class.

  1. public class IllegalInputException : ApplicationException
  2. {
  3. }

Now we need to create the instance variables to be used in your exception. We’ll need one for the severity level, one for the log level, one for the inner exception and one for the message.

  1. private int severityLevelOfException;
  2. private int logLevelOfException;
  3. private Exception innerException;
  4. private string customMessage;

The severity and log levels allow you to give a bit more information about the type and severity of the exception you are throwing. If your exception was caused by another exception, it will store the original exception data as the inner exception. Last, the custom message is what you put in it to be used when it is caught.

Even though there are 4 instance variables, the only one you will make public is the custom message.

  1. public string CustomMessage
  2. {
  3.  
  4. get {return this.customMessage; }
  5. set {this.customMessage = value; }
  6.  
  7. }

This will allow you to change the custom message after the exception has been instantiated. Now you’ll make the constructors for your custom exception. I created a default constructor as well as two overloaded constructors.

  1. public IllegalArgumentExpection( )
  2. {
  3.  
  4. this.CustomMessage = “Illegal Argument Exception.”;
  5.  
  6. }
  7.  
  8. public IllegalArgumentExpection(string customMessage)
  9. {
  10.  
  11. this.CustomMessage = customMessage;
  12.  
  13. }
  14.  
  15. public IllegalArgumentExpection(SeverityLevel severityLevel, LogLevel logLevel, Exception exception, string customMessage)
  16. {
  17.  
  18. this.severityLevelOfException = severityLevel;
  19. this.logLevelOfException = logLevel;
  20. this.innerException = exception;
  21. this.CustomMessage = customMessage;
  22.  
  23. }

The custom exception class is done now. Go to the Build menu and click Build Solution. The build should be successful and so your .dll library will be made. It’s ready to be used in any of your .Net applications now. In the project you plan on using it in, go to the Project menu and click Add Reference. It will open the Add Reference window where you click the Browse button. You can browse to custom exceptions project where your .dll should be in the \bin\Debug\ folder. Double click on it and it will be added to your project. You then need to add the using statement at the top of your class for it.

  1. using CustomExceptions;

I used CustomExceptions because that’s the namespace I used in my exceptions project. Once the using statement is in, you can throw your exception like it’s going out of style.

  1. throw new IllegalArgumentExpection(“I’m throwing an illegal argument exception.”);

You’re done. Wasn’t as bad as you thought I’m sure. We are using this method in both BG Alert, RemoteLMS and DesktopLMS. If you have any questions, email me at mike@therealgromer.com.

Post a comment
Name: 
Email: 
URL: 
Comments: