Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inherited Exception class with custom Message property

public class GroupWithSpecificOptionsNotFoundException : Exception
{
    public GroupWithSpecificOptionsNotFoundException(string place, Dictionary<string, string> options)
            : base(string.Format("Group with specific options not found, in ({0}), at ({1})", place, DateTime.Now.ToString()))
    {
        foreach (string key in options.Keys)
        {
            this.Message += string.Format("Option Name : ({0}) with Value : ({1}) not found in this group options set", key, options[key]);
        }

    }
}

The idea is simple, I want to include the key/value objects to the exception's Message. This action can't be performed in the base() thing, nor inside the constructor ("Message is read only").

I found a solution where a static function can do the trick:

public class GroupWithSpecificOptionsNotFoundException : Exception
{
    public static string GenerateOptionValueString(Dictionary<string, string> options)
    {
        string msg = string.Empty;
        foreach (string key in options.Keys)
        {
            msg += string.Format("Option Name : ({0}) with Value : ({1}) not found in this group options set", key, options[key]);
        }
        return msg;
    }

    public GroupWithSpecificOptionsNotFoundException(string place, Dictionary<string, string> options)
            : base (string.Format("Group with specific options not found ({2}), in ({0}), at ({1})",
                    place, DateTime.Now.ToString(), GroupWithSpecificOptionsNotFoundException.GenerateOptionValueString(options)))
    {
    }
}

But I'm not quite satisfied with it! Are there any other workarounds for this and similar cases?

like image 897
Rami Alshareef Avatar asked Apr 19 '11 08:04

Rami Alshareef


People also ask

How do you customize an exception message in Java?

In order to create custom exception, we need to extend Exception class that belongs to java.lang package. Consider the following example, where we create a custom exception named WrongFileNameException: public class WrongFileNameException extends Exception { public WrongFileNameException(String errorMessage) {

How do I create a custom exception class?

Steps to create a Custom Exception with an ExampleCreate one local variable message to store the exception message locally in the class object. We are passing a string argument to the constructor of the custom exception object. The constructor set the argument string to the private string message.

Which class can custom exception inherit C#?

When creating custom exception classes, they should inherit from the System. Exception class (or any of your other custom exception classes from the previous section). The class name should end with the word Exception, and it should implement at least the three common constructors of exception types.

What is a custom exception class?

The purpose of a custom exception class is to integrate the look-up of localized message strings in a custom message catalog into the mechanism that is used for error reporting in the client infrastructure.


1 Answers

General guidelines of writing exceptions states that you should write Exception with 3 common ctors. For example:

public class MyException : Exception
{
    public MyException()
    {
    }
    public MyException(string message) : base(message)
    {
    }
    public MyException(string message, Exception innerException) 
        : base(message, innerException)
    {
    }
}

I think that you simply need factory which will handle creation of exception with custom message.

like image 75
MichaelMocko Avatar answered Oct 24 '22 15:10

MichaelMocko