-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move error.jl docs inline; add example to catch_backtrace() #18961
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,24 +18,83 @@ | |
|
||
## native julia error handling ## | ||
|
||
""" | ||
error(message::AbstractString) | ||
|
||
Raise an `ErrorException` with the given message. | ||
""" | ||
error(s::AbstractString) = throw(ErrorException(s)) | ||
error(s...) = throw(ErrorException(Main.Base.string(s...))) | ||
|
||
""" | ||
rethrow([e]) | ||
|
||
Throw an object without changing the current exception backtrace. The default argument is | ||
the current exception (if called within a `catch` block). | ||
""" | ||
rethrow() = ccall(:jl_rethrow, Bottom, ()) | ||
rethrow(e) = ccall(:jl_rethrow_other, Bottom, (Any,), e) | ||
|
||
""" | ||
backtrace() | ||
|
||
Get a backtrace object for the current program point. | ||
""" | ||
backtrace() = ccall(:jl_backtrace_from_here, Array{Ptr{Void},1}, (Int32,), false) | ||
|
||
""" | ||
catch_backtrace() | ||
|
||
Get the backtrace of the current exception, for use within `catch` blocks. | ||
The backtrace is represented as an `Array{Ptr{Void}}` of addresses, so [`showerror`](:func:`showerror`) | ||
may be helpful for displaying the backtrace in a human-readable format. | ||
|
||
# Example | ||
|
||
```jldoctest | ||
julia> f(x) = try | ||
sqrt(x) | ||
catch y | ||
bt = catch_backtrace() | ||
showerror(STDOUT, y, bt) | ||
end; | ||
|
||
julia> f(-1) | ||
DomainError: | ||
in f(::Int64) at ./REPL[24]:2 | ||
in eval(::Module, ::Any) at ./boot.jl:236 | ||
... | ||
``` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if the output of calling say There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The output seems system dependent. I get
Is there a work-around/way to exclude local paths so I can include a doctest? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think those There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added the doctest without |
||
""" | ||
catch_backtrace() = ccall(:jl_get_backtrace, Array{Ptr{Void},1}, ()) | ||
|
||
## keyword arg lowering generates calls to this ## | ||
kwerr(kw, args...) = throw(MethodError(typeof(args[1]).name.mt.kwsorter, (kw,args...))) | ||
|
||
## system error handling ## | ||
""" | ||
systemerror(sysfunc, iftrue) | ||
|
||
Raises a `SystemError` for `errno` with the descriptive string `sysfunc` if `iftrue` is `true`. | ||
""" | ||
systemerror(p, b::Bool; extrainfo=nothing) = b ? throw(Main.Base.SystemError(string(p), Libc.errno(), extrainfo)) : nothing | ||
|
||
## assertion functions and macros ## | ||
|
||
""" | ||
assert(cond) | ||
|
||
Throw an [`AssertionError`](:obj:`AssertionError`) if `cond` is `false`. | ||
Also available as the macro `@assert expr`. | ||
""" | ||
assert(x) = x ? nothing : throw(Main.Base.AssertionError()) | ||
|
||
""" | ||
@assert cond [text] | ||
|
||
Throw an `AssertionError` if `cond` is `false`. Preferred syntax for writing assertions. | ||
Message `text` is optionally displayed upon assertion failure. | ||
""" | ||
macro assert(ex, msgs...) | ||
msg = isempty(msgs) ? ex : msgs[1] | ||
if !isempty(msgs) && (isa(msg, Expr) || isa(msg, Symbol)) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does this doctest pass like this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, I made a mistake. As you suggested earlier, the output from running doctests is different from what you get at the repl.
Additionally, it looks like I need to add a
println()
statement to get the doctest to pass. For example,and
pass, but they fail when I remove
println()
. Do you think either of the above is an adequate solution?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's strange. what does running the doctests say for "got" without the
println
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Buffering issue?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tkelman Excluding the system dependent info from "got", I see
I haven't been able to identify additional or missing whitespace as the problem.
@yuyichao Sorry, what do you mean?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was referring to output that included local paths. The full output is:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That should be included since it at least means what tony said won't work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the doctest system specifically removes those, but I'm not sure where that happens
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doctest fails even when I include the full output listed for "got".
After formatting this doctest several different ways, the only solutions I found involved adding calls to
println()
. I suspect there may be a bug in the way the doctest system is parsing this code block, but I did not find anything suspicious after going through what I could find of the documentation code. If there aren't other suggestions for things I might try, then I think I will move on to other fronts. Please let me know if you'd like me to convert this doctest to a regular code block or make some other modification. Thanks! :)