I'm not understanding the compile errors I'm getting when I try to set the following constructor for a custom exceptions class:
[Serializable]
class AuthenticationException : Exception
{
public int PracticeID { get; set; }
public AuthenticationException()
: base() { }
public AuthenticationException(string message)
: base(message) { }
public AuthenticationException(string message, Exception InnerException)
: base(message, InnerException) { }
public AuthenticationException(SerializationInfo info, StreamingContext context)
: base(info, context) { }
public AuthenticationException(int PracticeID)
: base(PracticeID)
{
this.PracticeID = PracticeID;
}
}
The errors I'm getting are as follows:
The best overloaded method match for 'System.Exception.Exception(string)' has some invalid arguments
&
cannot convert from 'int' to 'string'
Both are occurring at the : base(PracticeID) portion of the class.
I don't understand why it's looking for a string here.
I tried searching for an answer and cam upon these two previously asked questions
Custom exception with properties
What is the correct way to make a custom .NET Exception serializable?
I'm not sure what I'm doing differently from the first one that is causing the errors, and I tried reading/ replicating the second but I got completely lost.
This exception would occur in an inner loop, and I would like to have a customer error catch block on outer loop that deals with this particular situation.
I figure a workaround is just to use the Data property of the exception class and in the catch block of the outer loop check if there's a key for an item named something like "Authenticate" and handle the exception there.
I don't want to do that though because that sort exception handling is what custom exception classes are meant to do.
The base exception class doesn't have a matching constructor.
Instead, change your code to call the empty constructor (option A), or maybe use the id to provide a default error message (option B):
[Serializable]
class AuthenticationException : Exception
{
public int PracticeId { get; }
// Other constructors
// Option A
public AuthenticationException(int practiceId)
: base()
{
PracticeId = practiceId;
}
// Option B
public AuthenticationException(int practiceId)
: base("Invalid id: " + practiceId)
{
PracticeId = practiceId;
}
}