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
  • Loading branch information
asterite committed May 30, 2016
1 parent 66f4312 commit b3943d5
Show file tree
Hide file tree
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
Expand Up @@ -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)
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/raise.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b3943d5

Please sign in to comment.