Spargine: Throwing Exceptions Made Easy in Microsoft .NET

Once, while inspecting the source code for Microsoft .NET, I came across the use of a common class to simplify the process of throwing exceptions. Inspired by this concept, I decided to implement a similar approach in Spargine’s DotNetTips.Spargine.6.Core assembly, to make it easier for both myself and others to throw exceptions. In this article, I will outline the methods available in Spargine for throwing exceptions, accompanied by relevant examples.

In coding, there are many reasons why the code should throw exceptions. Here are a few of those reasons.

  • Error Handling: Developers throw exceptions to handle errors that occur during the execution of a program. For example, if a method is expecting a certain input parameter but receives an invalid parameter value, the method can throw an exception to indicate that an error has occurred. By throwing an exception, the method can provide an error message or code that can be used to identify the problem and take appropriate action.
  • Debugging: Developers throw exceptions to help them debug their code. By throwing an exception at a specific point in the code, developers can stop the execution of the program and examine the state of the application. This can help developers identify the cause of a problem and fix it.
  • Communication: Exceptions provide a way for developers to communicate with other developers or users of their applications. By throwing an exception with a descriptive error message, developers can provide information about what went wrong and how to fix it.
  • Robustness: Throwing exceptions can help make code more robust by handling unexpected situations. By catching exceptions and handling them appropriately, developers can prevent the program from crashing or producing incorrect.

The Exception Thrower

The class that contains all the methods for throwing exceptions is called ExceptionThrower.cs, and it includes both the common Exception types in .NET and the custom exceptions available in Spargine. Here is a description of all the currently available methods.

Each method allows for a custom message to be included. If no message is provided, a default message will be used.

Argument Exceptions

As I write often in articles, all data coming into a method or property should be validated before setting it to internal variables or using them in the method. Here are those methods.

  • ThrowArgumentException(message, innerException): Message defaults to “Invalid argument.”
  • ThrowArgumentInvalidException(message, paramName): Message defaults to “Invalid argument.” ArgumentInvalidException is a custom Exception type in Spargine.
  • ThrowArgumentInvalidException(message, paramName, innerException): Message defaults to “Invalid argument.” ArgumentInvalidException is a custom Exception type in Spargine.
  • ThrowArgumentNullException(message, innerException): Message defaults to “Argument cannot be null.”
  • ThrowArgumentNullException(message, paramName): Message defaults to “Argument cannot be null.”
  • ThrowArgumentNullException(paramName): Message defaults to “Argument cannot be null.”
  • ThrowArgumentOutOfRangeException(message, innerException): Message defaults to “Argument is out of the given range.”
  • ThrowArgumentOutOfRangeException(message, paramName): Message defaults to “Argument is out of the given range.”
  • ThrowArgumentOutOfRangeException(paramName) : Message defaults to “Argument is out of the given range.”
  • ThrowArgumentReadOnlyException(message, paramName): Message defaults to “Argument is out of the given range.” ArgumentReadOnlyException is a custom Exception type in Spargine.

Here is an example of how to use these methods.

IO Exceptions

These are methods for common IO exceptions.

  • ThrowDirectoryNotFoundException(message, directory): Message defaults to “The specified directory was not found.” DirectoryNotFoundException is a custom Exception type in Spargine.
  • ThrowDirectoryNotFoundException(message, innerException): Message defaults to “The specified directory was not found.” DirectoryNotFoundException is a custom Exception type in Spargine.
  • ThrowFileNotFoundException(message, filename): Message defaults to “File not found.”
  • ThrowFileNotFoundException(message, filename, innerException): Message defaults to “File not found.”
  • ThrowFileNotFoundException(message, innerException):  Message defaults to “File not found.”

Here is an example of how to use these methods.

Other Exceptions

  • ThrowInvalidCastException(message, innerException): Message defaults to “The object could not be cast to a new type.”
  • ThrowInvalidCastException(message, paramName):  Message defaults to “The object could not be cast to a new type.”
  • ThrowInvalidEnumTypeException(message):  Message defaults to “Invalid enum type.”
  • ThrowInvalidEnumTypeException(message, innerException): Message defaults to “Invalid enum type.”
  • ThrowInvalidOperationException(message): Message defaults to “Invalid operation.”
  • ThrowInvalidOperationException(message, innerException): Message defaults to “Invalid operation.”
  • ThrowInvalidValueException(message, value): Message defaults to “Invalid Value.”
  • ThrowInvalidValueException(message, value, innerException): Message defaults to “Invalid Value.”
  • ThrowMessageNotQueuedException(message): Message defaults to “The message could not be queued.” MessageNotQueuedException is a custom Exception type in Spargine.
  • ThrowMessageNotQueuedException(message, exception, userMessage): Message defaults to “The message could not be queued.” MessageNotQueuedException is a custom Exception type in Spargine.
  • ThrowMessageNotQueuedException(message, exception, userMessage): Message defaults to “The message could not be queued.” MessageNotQueuedException is a custom Exception type in Spargine.
  • ThrowMessageNotQueuedException(message, innerException): Message defaults to “The message could not be queued.” MessageNotQueuedException is a custom Exception type in Spargine.
  • ThrowMessageNotQueuedException(message, messageId): Message defaults to “The message could not be queued.” MessageNotQueuedException is a custom Exception type in Spargine.
  • ThrowMessageNotQueuedException(message, messageId, innerException): Message defaults to “The message could not be queued.” MessageNotQueuedException is a custom Exception type in Spargine.

Here is an example of how to use these methods.

Networking Exceptions

These three methods throw the Spargine exception type called NetworkConnectionException in case there is an issue connecting to Bluetooth, cellular, Ethernet, or WiFi. The third method below uses the NetworkConnection type in Spargine, where “connection” refers to the NetworkConnection type.

  • ThrowNetworkConnectionException(message): Message defaults to “Unknown network connection issue.”
  • ThrowNetworkConnectionException(message, connection): Message defaults to “Unknown network connection issue.”
  • ThrowNetworkConnectionException(message, innerException): Message defaults to “Unknown network connection issue.”

Summary

I recommend using ExceptionThrower in your projects to simplify the process of throwing exceptions. If you need an additional exception type added to this class, please leave a comment below. I welcome any questions or feedback you may have, so feel free to leave a comment as well. Your input is greatly appreciated!

2 thoughts on “Spargine: Throwing Exceptions Made Easy in Microsoft .NET

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.