how to get error line information when TCL_EvalFile return error 
Author Message
 how to get error line information when TCL_EvalFile return error

hi , when i use TCL_EvalFile to load a tcl file, if  this function
return TCL_ERROR ,  how can i get error line information in that file?

thanks very much



Mon, 29 Sep 2008 08:37:49 GMT  
 how to get error line information when TCL_EvalFile return error

Quote:

> hi , when i use TCL_EvalFile to load a tcl file, if  this function
> return TCL_ERROR ,  how can i get error line information in that file?

    code = Tcl_EvalFile(interp, filename);
    if (code == TCL_ERROR) {
        int lineNumber = interp->errorLine;
    }

But this raises the question what good this value will do for you?
If you're looking to format the value into a detailed error message
to display as a debugging aid, be aware that Tcl has already done
that for you:

   code = Tcl_EvalFile(interp, filename);
   if (code == TCL_ERROR) {
        const char *trace = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
   }

The variable "trace" now points to a character string with contents that
are a formatted description of the error and its context, for example:

wrong # args: should be "set varName ?newValue?"
    while executing
"set a b c"
    (file "foo.tcl" line 1)

which I assume contains the same sorts of information you wish to
present.  For the most part, I'd say Tcl's formatted the $::errorInfo
for you already, so just use it.

If you don't use "trace" right away, make a copy before making any more calls
into the Tcl library.  It points to memory that Tcl controls and may change,
or even free.

Starting in Tcl 8.5, there's another alternative:

    code = Tcl_EvalFile(interp, filename);
    if (code == TCL_ERROR) {
        Tcl_Obj *options = Tcl_GetReturnOptions(interp, code);
        Tcl_Obj *key = Tcl_NewStringObj("-errorline", -1);
        Tcl_Obj *value;
        int lineNumber;

        Tcl_DictObjGet(NULL, options, key, &value);
        Tcl_GetIntFromObj(NULL, value, &lineNumber);

        Tcl_DecrRefCount(key);
        Tcl_DecrRefCount(options);
    }

Finally, I'd note that I think you'd be better off using the
[source] and [catch] commands at script level than doing any
of this business in C.

    if {[catch {source foo.tcl} msg] == 1} {
        puts $::errorInfo
    }

or

    package require Tcl 8.5
    if {[catch {source foo.tcl} msg options] == 1} {
        set lineNumber [dict get $options -errorline]
        puts [dict get $options -errorinfo]
    }

--
| Don Porter          Mathematical and Computational Sciences Division |

| http://math.nist.gov/~DPorter/                                  NIST |
|______________________________________________________________________|



Mon, 29 Sep 2008 12:26:06 GMT  
 how to get error line information when TCL_EvalFile return error
thank you very much

it's just i want to know :)



Tue, 30 Sep 2008 08:33:57 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Returning error information from C

2. Embedding -- getting error/traceback information not to stderr?

3. Return Line Number for Error Handling

4. Getting line number for reporting error

5. getting Tcl file line number for error messages

6. Thorw an error or return an error code

7. Getting Error 31 and Error 21

8. ERROR MESSAGE: Internal Error: tpsbt.cpp line 2172

9. runtime error: Internal Error:tpsbt.cpp line 1482

10. Runtime Error / Internal Error: tpsbt: cpp line 2172

11. Tcl_EvalFile - how to get an error message

12. Generating errors in TCL script started with Tcl_EvalFile

 

 
Powered by phpBB® Forum Software