1

trying to create my first few custom exceptions in c# I dont really understand the MSDN articles and why they have multiple instances of the same

 public class CustomException : Exception
 {     
     public CustomException(): base() { }

     public CustomException(some message): base() { }
 }

etc etc

What I'd like to do is something like the following

 public class CustomException : Exception
 {     
     public AlreadySentException() : base () { }

     public InvalidMessageException() : base() { }
 }

I just want to be able to call these exceptions.

Ideas / recommendations on how to implement this would be greatly appreciated.

Thanks

1
  • 9
    That's not how classes work. Those are constructors.
    – SLaks
    Commented Jun 13, 2013 at 21:27

3 Answers 3

9

Define different exception classes. Simple as that!

    [Serializable]
    public class CustomException : Exception
    {
        public CustomException() { }
        public CustomException(string message) : base(message) { }
        public CustomException(string message, Exception inner) : base(message, inner) { }
        protected CustomException(
          System.Runtime.Serialization.SerializationInfo info,
          System.Runtime.Serialization.StreamingContext context)
            : base(info, context) { }
    }

    [Serializable]
    public class AlreadySentException : CustomException
    {
        public AlreadySentException() { }
        public AlreadySentException(string message) : base(message) { }
        public AlreadySentException(string message, Exception inner) : base(message, inner) { }
        protected AlreadySentException(
          System.Runtime.Serialization.SerializationInfo info,
          System.Runtime.Serialization.StreamingContext context)
            : base(info, context) { }
    }

    [Serializable]
    public class InvalidMessageException : CustomException
    {
        public InvalidMessageException() { }
        public InvalidMessageException(string message) : base(message) { }
        public InvalidMessageException(string message, Exception inner) : base(message, inner) { }
        protected InvalidMessageException(
          System.Runtime.Serialization.SerializationInfo info,
          System.Runtime.Serialization.StreamingContext context)
            : base(info, context) { }
    }
2
  • 2
    Don't forget that Visual Studio has spippet for exceptions. Just type exception and press Tab Commented Jun 13, 2013 at 21:33
  • @NechytailoOleh this is exactly how this example was made ;)
    – Odys
    Commented Jun 13, 2013 at 21:34
1

Exceptions are simply classes derived from the base Exception class.

public class CustomException(): base()
{
}

public class CustomException(some message): base()
{
}

That's not valid c# code. But they look like they could be constructors:

public CustomException(): base()
{
}

public CustomException(some message): base()
{
}

These are different constructors to one CustomException class. You'd construct your class using one of these depending on if you want to include a message or not. But they both result in the same exception.

To define your desired exceptions, you'd derive each one from Exception:

public class AlreadySentException : Exception { }

public class InvalidMessageException : Exception { }

If you're not comfortable with class inheritance, I recommend you brush up on that first.

0
1
public CustomException(): base()
{
}

public CustomException(some message): base()
{
}

Those are overloaded constructors, they refer to the same object; you can instantiate the CustomException class by calling either new CustomException() or CustomException("some string").

What you need, is different classes, like this :

public class AlreadySentException : Exception
{
}

public class InvalidMessageException: Exception
{
}

You can use overloading with theses classes too. Let's say want to have a default message, or a custom one, you can do this :

public class AlreadySentException : Exception
{
    public AlreadySentException() : base("Already sent"){}
    public AlreadySentException(string message) : base(message){}
}

It will call the overloaded version of the Exception base class, which populates the Message property.

1
  • OP seems to need AlreadySent and InvalidaMessage exception to derive from his Custom exception.
    – Odys
    Commented Jun 13, 2013 at 21:35

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.