如何在 Julia 中找到导致错误的行?
How to find the line causing error in Julia?
假设在 Julia 中有一个调用函数 B 的脚本 A。
函数 B 中存在一些错误,导致脚本在运行时停止。
有没有一种巧妙的方法可以找出导致错误的行?
没有任何意义,必须在每一行中手动输入诸如println之类的消息,以找出代码存活到哪一行,以及哪一行发生错误。
编辑:我正在使用 Linux Red Hat 4.1.2 和 Julia 版本 0.3.6。直接地。没有 IDE.
读取回溯:
juser@juliabox:~$ cat foo.jl
# line 1 empty comment
foo() = error("This is line 2")
foo() # line 3
juser@juliabox:~$ julia foo.jl
ERROR: This is line 2
in foo at /home/juser/foo.jl:2
in include at ./boot.jl:245
in include_from_node1 at loading.jl:128
in process_options at ./client.jl:285
in _start at ./client.jl:354
while loading /home/juser/foo.jl, in expression starting on line 3
这行 in foo at /home/juser/foo.jl:2 ... while loading /home/juser/foo.jl, in expression starting on line 3
读作:"there was an error at line 2
in /home/juser/foo.jl
file ... while loading /home/juser/foo.jl
, in expression starting on line 3
"
我看起来很清楚!
编辑: /home/juser/foo.jl:2
表示;文件:/home/juser/foo.jl
,行号:2
.
您也可以使用 @show
宏而不是 println
函数来进行调试:
julia> println(1 < 5 < 10)
true
julia> @show 1 < 5 < 10
(1<5<10) => true
true
假设在 Julia 中有一个调用函数 B 的脚本 A。 函数 B 中存在一些错误,导致脚本在运行时停止。 有没有一种巧妙的方法可以找出导致错误的行?
没有任何意义,必须在每一行中手动输入诸如println之类的消息,以找出代码存活到哪一行,以及哪一行发生错误。
编辑:我正在使用 Linux Red Hat 4.1.2 和 Julia 版本 0.3.6。直接地。没有 IDE.
读取回溯:
juser@juliabox:~$ cat foo.jl
# line 1 empty comment
foo() = error("This is line 2")
foo() # line 3
juser@juliabox:~$ julia foo.jl
ERROR: This is line 2
in foo at /home/juser/foo.jl:2
in include at ./boot.jl:245
in include_from_node1 at loading.jl:128
in process_options at ./client.jl:285
in _start at ./client.jl:354
while loading /home/juser/foo.jl, in expression starting on line 3
这行 in foo at /home/juser/foo.jl:2 ... while loading /home/juser/foo.jl, in expression starting on line 3
读作:"there was an error at line 2
in /home/juser/foo.jl
file ... while loading /home/juser/foo.jl
, in expression starting on line 3
"
我看起来很清楚!
编辑: /home/juser/foo.jl:2
表示;文件:/home/juser/foo.jl
,行号:2
.
您也可以使用 @show
宏而不是 println
函数来进行调试:
julia> println(1 < 5 < 10)
true
julia> @show 1 < 5 < 10
(1<5<10) => true
true