JvInterpreterでDelphi/C++Builderアプリケーションに簡易Pascal言語を組み込む(3)

コードを検証する

JvInterpreterProgramのCompileメソッドを実行すると、コードに問題があるときにEJvInterpreterError例外が投げられます。

EJvInterpreterErrorのErrCodeプロパティとMessageプロパティで、エラーの内容を取得できます。

JvInterpreterProgramのLastError.ErrCodeプロパティやLastError.Messageでもエラーの内容を取得でき.LastError.ErrPosプロパティでエラーの場所がわかります。

begin
  try
    JvInterpreterProgram1.Compile;
  except
    on E : EJvInterpreterError do
    begin
      ShowMessage(Format('%d:%s', [E.ErrCode, E.Message]));
      if E.ErrPos > -1 then
      begin
        Memo1.Text := JvInterpreterProgram1.Pas.Text;
        Memo1.SelStart := E.ErrPos;
        Memo1.SelLength := 0;
      end;
    end;
    on E: Exception do
    begin
      ShowMessage(Format('%d:%s', [
        JvInterpreterProgram1.LastError.ErrCode,
        JvInterpreterProgram1.LastError.Message]));
      if JvInterpreterProgram1.LastError.ErrPos > -1 then
      begin
        Memo1.Text := JvInterpreterProgram1.Pas.Text;
        Memo1.SelStart := JvInterpreterProgram1.LastError.ErrPos;
        Memo1.SelLength := 0;
      end;
    end;
  end;

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください