Skip to content

Commit

Permalink
Set Exception's callstack when raising it, not when creating it
Browse files Browse the repository at this point in the history
Ary Borenszweig committed May 30, 2016

Verified

This commit was signed with the committer’s verified signature.
jhass Jonne Haß
1 parent 66f4312 commit b3943d5
Showing 2 changed files with 9 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/exception.cr
Original file line number Diff line number Diff line change
@@ -143,15 +143,17 @@ end
class Exception
getter message : String?
getter cause : Exception?
property callstack : CallStack?

def initialize(message : String? = nil, cause : Exception? = nil)
@message = message
@cause = cause
@callstack = CallStack.new
def initialize(@message : String? = nil, @cause : Exception? = nil)
end

def backtrace
@callstack.printable_backtrace
self.backtrace?.not_nil!
end

def backtrace?
@callstack.try &.printable_backtrace
end

def to_s(io : IO)
@@ -166,7 +168,7 @@ class Exception

def inspect_with_backtrace(io : IO)
io << @message << " (" << self.class << ")\n"
backtrace.each do |frame|
backtrace.try &.each do |frame|
io.puts frame
end
io.flush
1 change: 1 addition & 0 deletions src/raise.cr
Original file line number Diff line number Diff line change
@@ -96,6 +96,7 @@ fun __crystal_get_exception(unwind_ex : LibUnwind::Exception*) : UInt64
end

def raise(ex : Exception) : NoReturn
ex.callstack = CallStack.new
unwind_ex = Pointer(LibUnwind::Exception).malloc
unwind_ex.value.exception_class = LibC::SizeT.zero
unwind_ex.value.exception_cleanup = LibC::SizeT.zero

0 comments on commit b3943d5

Please sign in to comment.