I'm currently learning C#, but I am stuck on something and I can't find a solution for it.
I am trying to create my own Exception class.
The exception is called "InvalidNumberException", it checks if a number is equal to 5. I know it may seem kinda stupid, but I just need to get the idea about creating Custom Exceptions.
So far I found on MSDN that for creating the Exception I need these four constructors:
public class InvalidNumberException : System.Exception
{
public InvalidNumbertException() : base() { }
public InvalidNumberException(string message) : base(message) { }
public InvalidNumberException(string message, System.Exception inner) : base(message, inner) { }
// A constructor is needed for serialization when an
// exception propagates from a remoting server to the client.
protected InvalidNumberException(System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context) { }
}
but I don't know how to implement the method or constructor in this class that a number entered from the console is equal to 5, and if it is not, it throws an exception.
I'll appreciate if someone helps me with this.
public class InvalidNumberException : Exception {}should work if all you're ever going to do isthrow new InvalidNumberException();