Delphi Error – E2145 Re-raising an exception only allowed in exception handler

Delphi Compiler Error

E2145 Re-raising an exception only allowed in exception handler

Reason for the Error & Solution

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.

Share:

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

Delphi Compiler Error X2421 Imported identifier ‘%s’ conflicts with ‘%s’ in ‘%s’ Reason for the Error & Solution This occurs...
Delphi Compiler Error X2367 Case of property accessor method %s.%s should be %s.%s Reason for the Error & Solution No...
Delphi Compiler Error X2269 Overriding virtual method ‘%s.%s’ has lower visibility (%s) than base class ‘%s’ (%s) Reason for the...