Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • Custom exception objects in .Net

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 17
    Comment on it
                       Custom exception objects in .Net:
    

    In order to make a custom exception we have to derive the System.Exception class. We can

    derive the custom exception directly from the System.Exception class or can use an

    intermediate exception like SystemException or ApplicationException as base class.

    Note: According to .NET framework documentation, SystemException is meant only for those

    exceptions defined by the common language runtime whereas ApplicationException is intended

    to be used by user code as ApplicationException is thrown by a user program, not by the

    common language runtime.

    For Example:- Let us create a custom exception for the login mechanism :

    The basic structure looks like:-

    public class LoginFailedException: System.Exception
    {
    
    }
    

    C# doesnt inherit constructors of base classes,So we need to add at least one constructor

    which does something useful:

    public class LoginFailedException: System.Exception
    {
    
       public LoginFailedException()
       {
    
       }
    
       public LoginFailedException(string message): base(message)
       {
    
       }
    }
    

    Now we can throw an instance of LoginFailedException and pass a message which describes

    the occurred error.

    Incase of Serialization: We need to implement the ISerializable interface (Since our base Exception class already implements the ISerializable interface we can omit it in our base classes),this interface contains a single

    method, named GetObjectData, which is responsible for storing object fields during the

    serialization process. Now when an object is being deserialized, the previously filled

    SerializationInfo object is passed to the constructor of the class in which we are

    responsible for restoring the original object fields.

    Example:

    [Serializable]
    public class LoginFailedException: System.Exception
    {
       private string fUserName;
    
       protected LoginFailedException(SerializationInfo info,StreamingContext context): base
    
    (info, context)
       {
          if (info != null)
          {
             this.fUserName = info.GetString("fUserName");
          }
       }
    
       public override void GetObjectData(SerializationInfo info,StreamingContext context)
       {
          base.GetObjectData(info, context);
    
          if (info != null)
          {
             info.AddValue("fUserName", this.fUserName);
          }
       }
    }
    
    C# asp.net C#

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: