newBike
newBike

Reputation: 15022

How to print exception and then keep sequential executing rather than jump to rescue section

I'm practicing the Ruby

I often put the wrong code in my file intentionally

e.g.Abc.hi

And the program will raise error then exit.

So I have to wrapped it with begin with block.

How could I let the wrong code only show the exceptions on the console, and keep doing the following code without wrapping with begin with block.

Thanks

require 'pry'

module Test
  def hi
    p "hihi"
  end
end

def Test.hello
  p "hello"
end

class Abc
  include Test
end
abc = Abc.new
begin
  Abc.hi
rescue Exception => e
  p e
end

binding.pry

Upvotes: 0

Views: 32

Answers (1)

Uri Agassi
Uri Agassi

Reputation: 37419

You can't do that, because that is the point of exceptions - they break the flow.

If you simply want to see the stack trace at that point, use caller

def a
  puts caller
end
def b
  a
end
def c
  b
end
c()
#=> prog:2:in `a'
#=> prog:5:in `b'
#=> prog:8:in `c'
#=> prog:10:in `<main>'

Upvotes: 1

Related Questions