Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to get the script line number when an exception occurs #491

Closed Answered by pyscripter
chicknsoup asked this question in Q&A
Discussion options

Hello,

When running a python script in background without TPythonGUIInputOutput, it's difficult to debug when exception occurs. In this case, how to get the stacktrace/line number?

Thank you

You must be logged in to vote

When you execute code, say with ExecString, CheckError is called at the end which on exception updates PythoneEngine.Traceback. and raises a suitable EPythonError descendent exception.

type
 EPythonError = class(Exception)
 public
 EName : string;
 EValue : string;
 end;
 TTracebackItem = class
 public
 FileName : string;
 LineNo : Integer;
 Context : string;
 end;
 TPythonTraceback = class
 public
 property ItemCount : Integer read GetItemCount;
 property Items[ idx : Integer ] : TTracebackItem read GetItem;
 property Limit : Integer read FLimit write FLimit;
 end;

So what you typically do is:

try
 ExecString(Code, FileName);
except
 ...

Replies: 1 comment 1 reply

Comment options

When you execute code, say with ExecString, CheckError is called at the end which on exception updates PythoneEngine.Traceback. and raises a suitable EPythonError descendent exception.

type
 EPythonError = class(Exception)
 public
 EName : string;
 EValue : string;
 end;
 TTracebackItem = class
 public
 FileName : string;
 LineNo : Integer;
 Context : string;
 end;
 TPythonTraceback = class
 public
 property ItemCount : Integer read GetItemCount;
 property Items[ idx : Integer ] : TTracebackItem read GetItem;
 property Limit : Integer read FLimit write FLimit;
 end;

So what you typically do is:

try
 ExecString(Code, FileName);
except
 on E: EPythonError do
 begin
 // Inspect E.EName and E.EValue
 // Inspect the GetPythonEngine.Traceback
 end;
end
You must be logged in to vote
1 reply
Comment options

Thank you.

Answer selected by chicknsoup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /