Return line number?

Monkey Forums/Monkey Programming/Return line number?

Richard Betson(Posted 2016) [#1]
Hi,

Is there a way to get the line number within the code? What I need to do is to be able to return (via Print()) the line number when an error occurs like for example an image was not loaded and is Null. I would like to give the framework I am building the ability to report it's own errors and print the line number in the Ted console.

Thanks.


therevills(Posted 2016) [#2]
I think you are going to be hard pressed to do it unless you write a mini-compiler yourself.

When MX compiles it knows what line it is compiling against and can report it straight away. At run time in debug the compiler inserts error log lines so when a runtime exception it outputs the line number from those log lines.

I think you should just throw an error when the image is null etc.


muddy_shoes(Posted 2016) [#3]
__LINE__ and __FILE__ macros should really be part of any compilation tool chain for just this sort of thing. It shouldn't be too difficult to add them into the Monkey compiler if you can spend an hour or two familiarising yourself with the code structure..


Gerry Quinn(Posted 2016) [#4]
You could return the method name and other information, though, which is probably just as informative as the line number (more informative sometimes,once you are no longer looking for bugs in a newly written method).


Richard Betson(Posted 2016) [#5]
You could return the method name and other information, though, which is probably just as informative as the line number (more informative sometimes,once you are no longer looking for bugs in a newly written method).

That might actually work out OK. What I am trying to accomplish is to give the coder the best shot at understanding what went wrong. At least pointing at the method through an error/warning message (also including the framework code file name generating error) in the console will get them pretty close to the error/warning source.

Having the ability to throw a line number on a custom error/warning (passive errors that may not trigger the compilers error catch) outside of the compiler would be cool and for modules/framework designers would be nifty. Maybe this something to pitch at Mark to add to Monkey2.