Lets say I would like to make some custom exceptions. And I would like to have more of them. I could create for every new exception a new class, but is there another way to do this? And if I have to create always a new class, where to store them? Its just looking not that nice in the root folder of a project.
Ahm and another question: what am I doing if some exceptions are the same, just the name of the exception is changing a little bit? Lets say exception A is looking like this:
[Serializable()]
public class ExceptionA: Exception, ISerializable
{
public ExceptionA() : base() { }
public ExceptionA(string message) : base(message) { }
public ExceptionA(string message, System.Exception inner) : base(message, inner) { }
public ExceptionA(SerializationInfo info, StreamingContext context) : base(info, context) { }
}
}
and another one is the same, just another name:
[Serializable()]
public class ExceptionB: Exception, ISerializable
{
public ExceptionB() : base() { }
public ExceptionB(string message) : base(message) { }
public ExceptionB(string message, System.Exception inner) : base(message, inner) { }
public ExceptionB(SerializationInfo info, StreamingContext context) : base(info, context) { }
}
}
And so on. Do I really have to create always a new class and paste the same code? Any suggestions?
No, there isn’t another way. If you want to create a new exception type, you need a new class for it.
In a location that is accessible to all the code that will need to use them.
Yep, still need to create a new class – though you can derive the new one from an existing exception class.