Reraising Exceptions

From RAD Studio
Jump to: navigation, search

Go Up to Writing Exception Handlers


Sometimes when you handle an exception locally, you want to augment the handling in the enclosing block, rather than replace it. Of course, when your local handler finishes its handling, it destroys the exception instance, so the enclosing block's handler never gets to act. You can, however, prevent the handler from destroying the exception, giving the enclosing handler a chance to respond. You do this by using the raise command with no arguments. This is called reraising or rethrowing the exception. The following example illustrates this technique:

 try
 { statements }
   try
 { special statements }
   except
     on ESomething do
     begin
 { handling for only the special statements }
       raise;{ reraise the exception }
     end;
   end;
 except
   on ESomething do ...;{ handling you want in all cases }
 end;

If code in the statements part raises an ESomething exception, only the handler in the outer exception-handling block executes. However, if code in the special statements part raises ESomething, the handling in the inner exception-handling block executes, followed by the more general handling in the outer exception-handling block. By reraising exceptions, you can easily provide special handling for exceptions in special cases without losing (or duplicating) the existing handlers.

If the handler wants to throw a different exception, it can use the raise or throw statement in the normal way, as described in Raising an Exception.

See Also