- Joined
- Sep 25, 2003
- Location
- Mesa, AZ
So question for some of you knowledgeable types out there...
I'm working on tweeking an exception handler class I'm writing. I originally implemented it in a longer if statement, and am working on refactoring it over to a switch statement. The compiler, however, doesn't seem to like what I'm telling it. Here's what I've got so far:
So like my comments say: The compiler error I'm getting is telling me that C# is looking for a different type in my switch statement. I'm understanding, if I want to go this route, I may need to cast my exception object to something else, either a primitive type (byte, long, int, etc) or cast to an enum.
Any thoughts? Am I barking up the right tree?
I'm working on tweeking an exception handler class I'm writing. I originally implemented it in a longer if statement, and am working on refactoring it over to a switch statement. The compiler, however, doesn't seem to like what I'm telling it. Here's what I've got so far:
Code:
public sealed class ExceptionHandler
{
private const string FORMAT = "Format Exception";
private const string OVERFLOW = "Overflow Exception";
private const string EXC = "Other Exception";
/// <summary>
/// Playing with switch statements...
/// </summary>
/// <param name="ex"></param>
/// <returns></returns>
public static string ValidateException(Exception ex)
{
/* Here's the effect I'm looking to duplicate via my switch statement:
if (ex is FormatException)
{
return FORMAT;
}
else if (ex is OverflowException)
{
return OVERFLOW;
}
else
{
return EXC;
}
*/
switch (ex) // Compiler is looking for a different datatype,
// most primitive types and enum types are ok...
// Might want to look at casting to a primitive or enum.
{
case (ex is FormatException):
return FORMAT;
break;
case (ex is OverflowException):
return OVERFLOW;
break;
default:
return EXC;
break;
}
}
So like my comments say: The compiler error I'm getting is telling me that C# is looking for a different type in my switch statement. I'm understanding, if I want to go this route, I may need to cast my exception object to something else, either a primitive type (byte, long, int, etc) or cast to an enum.
Any thoughts? Am I barking up the right tree?