Re: Custom Exception Handler Class

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: David Levine (noSpamdlevineNNTP2_at_wi.rr.com)
Date: 06/22/04


Date: Mon, 21 Jun 2004 19:21:44 -0500


"Trace" <Trace@discussions.microsoft.com> wrote in message
news:9842F6E4-0EF1-46AD-845A-0F6742BFC1D6@microsoft.com...
> 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"));



Relevant Pages

  • Re: Structured exception information
    ... Write_Output for different hardware will need need a corresponding ... version of the exception type; ... the messages are indeed string literals, ... The Ada compiler checks coverage. ...
    (comp.lang.ada)
  • Re: C++ in embedded systems
    ... linker and compiler work together flawlessly, ... when it has absolutely no idea what kind of exception handling ... programmer would imagine that the two calls to foowould take ... Providing an object constructor with a single parameter type may ...
    (comp.os.linux.embedded)
  • Re: OOP and OOD for senior C-style embedded software engineers
    ... metaprogramming in C++ to whole different level using the template ... templates let the compiler do more work ... The worst thing is when a function throws an exception ... And of course, exception specifications should work like people think they work, not as they /do/ work. ...
    (comp.arch.embedded)
  • Re: Reprise: in out parameters for functions
    ... one could have entries of multiple protected objects and tasks. ... > compiler checked your units for you, ... > You assumed that the root type for Exception would be tagged record. ... > package Crazy is ...
    (comp.lang.ada)
  • Re: Poll: Why you do not use Lisp?
    ... where neither my C compiler nor my JVM (more correctly, ... JBC-interpeter does its own internal exception handling, ... (FWIW, neither does SEH). ... partly implemented some of the mechanisms (I can unwind and do backtraces, ...
    (comp.programming)