11

After some research I found that a custom exception should look like this:

using System;
using System.Runtime.Serialization;

namespace YourNamespaceHere
{
    [Serializable()]
    public class YourCustomException : Exception, ISerializable
    {
        public YourCustomException() : base() { }
        public YourCustomException(string message) : base(message) { }
        public YourCustomException(string message, System.Exception inner) : base(message, inner) { }
        public YourCustomException(SerializationInfo info, StreamingContext context) : base(info, context) { }
    }
}

But I have small problem.

I want above exception to have two additional fields, say int ID and int ErrorCode. How do I add these two fields and initialize them - shall I add a new constructor, with these two parameters and the message parameter?

Also can you help me and show how to write the serialization methods for this new class which will have the two new properties?

Thank you.

  • 1
    Just add the properties to your class – blogbydev Oct 09 '15 at 06:48
  • found this http://blogs.msdn.com/b/agileer/archive/2013/05/17/the-right-way-to-write-a-custom-exception-class.aspx – blogbydev Oct 09 '15 at 06:53
  • @singsuyash: Shall I add *new* constructor which takes message and the two integers? It will be nice if someone can sum up those things and what you link as an answer for my class with those two integer properties. –  Oct 09 '15 at 07:01
  • Shetty added an example for you – blogbydev Oct 09 '15 at 08:46

1 Answers1

16

It will look something like this. You look for more details here What is the correct way to make a custom .NET Exception serializable?

 [Serializable()]
        public class YourCustomException : Exception, ISerializable
        {
            public Int Id { get; set; }
            public Int ErrorCode { get; set; }
            public YourCustomException() : base() { }
            public YourCustomException(string message) : base(message) { }
            public YourCustomException(string message, System.Exception inner) : base(message, inner) { }
            public YourCustomException(SerializationInfo info, StreamingContext context) : base(info, context) { }
            public YourCustomException(string message, int Id, int ErrorCode)
                : base(message)
            {
                this.Id = Id;
                this.ErrorCode = ErrorCode;
            }
        }
Community
  • 1
  • 1
Shetty
  • 1,792
  • 4
  • 22
  • 38
  • Can I add some custom methods to my exception class also? –  Oct 09 '15 at 08:02
  • After you catch your custom exception, you can call those methods. I would like to know what are going to do in that method. Are you ready to receive an unhandled exception or jump up to another catch if that method throws an exception. – blogbydev Oct 09 '15 at 08:46
  • @singsuyash: That method will probably only initialize member variables of some class object which will be passed to this method –  Oct 09 '15 at 09:43
  • @singsuyash can we implement this as an Attribute. – Zaker Apr 28 '18 at 08:18
  • @Zaker, sorry i have lost context on this one, ill get back in a few days. – blogbydev May 01 '18 at 14:47
  • Should the properties have public setters? – Hutch Nov 15 '18 at 01:14
  • @Shetty But how will you Catch those values? – Sarath Mohandas Jul 10 '20 at 09:32
  • You don't need to add `ISerializable`. `Exception` already implements this and you are inheriting from that. – bytedev Jan 20 '21 at 09:57
  • Your ctor with `(SerializationInfo info, StreamingContext context)` can be protected rather than public – bytedev Jan 20 '21 at 10:00