Custom Exception Handler Class

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am attempting to develop a custom class wich handles caught exceptions, much like the ExceptionManager. I am having difficulty in informing the compiler that my custom class will handle the exception appropriately.

For instance:

bool System.Collections.IDictionary.Contains(object key)
{
if(key is string)
return(this.Contains((string) key));
else
throw(new ArgumentException("key must be of type string.", "key"));
}

The above compiles just fine.

bool System.Collections.IDictionary.Contains(object key)
{
if(key is string)
return(this.Contains((string) key));
else
ExceptionHandler.Handle(new ArgumentException("key must be of type string.", "key"));
}

The above will not compile due to the fact that the compiler detect sthe execution path the does not provide a return.

I know I can modify the code to get this to work, but I was wondernig if anyone knows of an interface or something that my ExceptionHandler can use to inform the compiler that it will be stopping execution stream and can therfore compile the above code.
 
Trace said:
I am attempting to develop a custom class wich handles caught exceptions,
much like the ExceptionManager. I am having difficulty in informing the
compiler that my custom class will handle the exception appropriately.
For instance:

bool System.Collections.IDictionary.Contains(object key)
{
if(key is string)
return(this.Contains((string) key));
else
throw(new ArgumentException("key must be of type string.", "key"));
}

The above compiles just fine.

bool System.Collections.IDictionary.Contains(object key)
{
if(key is string)
return(this.Contains((string) key));
else
ExceptionHandler.Handle(new ArgumentException("key must be of type string.", "key"));
}

The above will not compile due to the fact that the compiler detect sthe
execution path the does not provide a return.
I know I can modify the code to get this to work, but I was wondernig if
anyone knows of an interface or something that my ExceptionHandler can use
to inform the compiler that it will be stopping execution stream and can
therfore compile the above code.

There's no way to tell the compiler to treat a method call like a return
statement, but you can define your method as:

public System.Exception ExceptionHandler.Handle(System.Exception
innerException); // returns an exception object

then you can write:

if(key is string)
return(this.Contains((string) key));
else
throw ExceptionHandler.Handle(new ArgumentException("key must be of
type string.", "key"));
 
Back
Top