Where / how to store or handle many custom exceptions?

Suppose I would like to make some custom exceptions. And I would like to have more. I can create a new class for each new exception, but is there another way to do this? And if I need to always create a new class, where to store them? Its just not looking so good in the project root folder.

Ahm and another question: what do I do if some exceptions are the same, only the name of the exception changes a little? Assume exception A is as follows:

[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 the other is one and the same, 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 need to always create a new class and paste the same code? Any suggestions?

+4
source share
3 answers

I can create a new class for each new exception, but is there another way to do this?

No, there is no other way. If you want to create a new type of exception, you need a new class for this.

where to store them?

In a place accessible to all the code that will have to use them.

What do I do if some exceptions are the same, only the name of the exception changes a little?

Yes, you still need to create a new class - although you can get a new one from an existing exception class.

+7
source

As a rule, for something like this, I would create a single project space or name that will contain all these user exceptions (or even enumerations) that will be used throughout the project / solution.

This gives me the only link / assembly point for the link.

In addition to your question about exceptions with the same “look,” I would create a base class that can be inherited by other exceptions with “slightly different” names.

+1
source

You have to throw a new exception every time, because that is how catch () works in try ... catch block to catch a specific type of exception. But I really like the following approach, taken from the CLR through a C # book:

 public abstract class ExceptionArgs { public string Message { get { return string.Empty; } } } [Serializable] public class Exception<TExceptionArgs> : Exception, ISerializable where TExceptionArgs : ExceptionArgs { private const String c_args = "Args"; // For (de)serialization private readonly TExceptionArgs m_args; public TExceptionArgs Args { get { return m_args; } } public Exception(String message = null, Exception innerException = null) : this(null, message, innerException) { } public Exception(TExceptionArgs args, String message = null, Exception innerException = null) : base(message, innerException) { m_args = args; } [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)] protected Exception(SerializationInfo info, StreamingContext context) : base(info, context) { m_args = (TExceptionArgs)info.GetValue(c_args, typeof(TExceptionArgs)); } [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)] public override void GetObjectData(SerializationInfo info, StreamingContext context) { info.AddValue(c_args, m_args); base.GetObjectData(info, context); } public override String Message { get { String baseMsg = base.Message; return (m_args == null) ? baseMsg : baseMsg + " (" + m_args.Message + ")"; } } public override Boolean Equals(Object obj) { Exception<TExceptionArgs> other = obj as Exception<TExceptionArgs>; if (obj == null) return false; return Object.Equals(m_args, other.m_args) && base.Equals(obj); } public override int GetHashCode() { return base.GetHashCode(); } } 

Now you can create new arguments for such exceptions:

 public class ExceptionAExceptionArgs : ExceptionArgs { //may add some properties if required here } 

And catch the “new exceptions” as follows:

 try { //do something here... } catch (Exception<ExceptionAExceptionArgs> ex) { } 

I will make you write less code to create new exceptions.
Please note that this approach will not work if you want to create hiearachy exceptions where there is one base exception class and several children.
You can put exception classes in which you want them to be available, but this is a good approach to create a separate file for each.

+1
source

Source: https://habr.com/ru/post/1432700/


All Articles