E2145 Re-raising an exception only allowed in exception handler (Object Pascal)

From Appmethod Topics
Jump to: navigation, search

Go Up to Error and Warning Messages (Object Pascal) Index

You have used the syntax of the raise statement which is used to reraise an exception, but the compiler has determined that this reraise has occurred outside of an exception handler block. A limitation of the current exception handling mechanism disallows reraising exceptions from nested exception handlers. for the exception.


program Produce;

  procedure RaiseException;
  begin
    raise;            (*case 1*)
    try
      raise;          (*case 2*)
    except
      try
        raise;        (*case 3*)
      except
      end;
      raise;
    end;
  end;


begin
end.

There are several reasons why this error might occur. First, you might have specified a raise with no exception constructor outside of an exception handler. Secondly, you might be attempting to reraise an exception in the try block of an exception handler. Thirdly, you might be attempting to reraise the exception in an exception handler nested in another exception handler.


program Solve;
  uses SysUtils;

  procedure RaiseException;
  begin
    raise Exception.Create('case 1');
    try
      raise Exception.Create('case 2');
    except
      try
        raise Exception.Create('case 3');
      except
      end;
      raise;
    end;
  end;

begin
end.

One solution to this error is to explicitly raise a new exception; this is probably the intention in situations like 'case 1' and 'case 2'. For the situation of 'case 3', you will have to examine your code to determine a suitable workaround which will provide the desired results.