본문 바로가기
조회 수 1771 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

+ - Up Down Comment Print
?

단축키

Prev이전 문서

Next다음 문서

+ - Up Down Comment Print

http://coderrr.wordpress.com/2008/11/07/the-simple-way-to-print-exceptions-in-ruby/


p $!, *$@ is a little more optimal in that you will see the class of the exception also.

I’ll put the meat of this post first so you don’t have to waste your time if you already know this. The simplest way to catch and print out an exception along with it’s backtrace:

1
2
3
4
5
6
7
8
9
10
11
12
13
# catch most exceptions (anything that derives from StandardError)
begin
  ...
rescue
  puts $!, $@
end
 
# catch all exceptions (anything that derives from Exception)
begin
  ...
rescue Exception
  puts $!, $@
end

At some point we all want to catch an exception, print it out, and then move on to something else. I just wanted to point out a very simple way to do this. I want to point this out because I have seen many people spend far too much typing on this (myself included). Here’s a combined example of the overtyping I’ve seen used.

1
2
3
4
5
6
begin
  ...
rescue Exception => ex
  puts ex.message
  puts ex.backtrace.join("\n")
end

First off, Ruby stores the last exception caught in the special (thread local) variable $! and the backtrace of the last exception in $@. So there is no need to explicitly specify a variable to store it in. Secondly,Exception#message in most cases is redundant because it’s just aliased to Exception#to_s which puts will call for you. Thirdly calling join("\n") is redundant when passing an array to puts. By default puts will output an array with a newline between each item. Also you might not have known that you can pass multiple arguments to puts and it will be just as if you had called puts for each one.

Usually the message of the exception is clear enough so you don’t need to see its class. But in the cases where you want to, we can easily show this too without much more work:

1
2
3
4
5
6
7
8
9
10
rescue
  p $!
  puts $@
end
 
# or
 
rescue
  puts $!.inspect, $@
end

p will call inspect on the exception which will show it’s class and message.

I know this is simple and trivial stuff but I still see a lot of people doing it a more verbose way than necessary. Some people will probably argue that it’s less understandable my way or we should be more explicit or something and that’s a matter of opinion but I personally disagree with it. Anyway this post is mainly for the people who would like doing it the shorter way but just didn’t know about it yet.



Title
List of Articles
번호 제목 글쓴이 날짜 조회 수
31 Different Ways to Set Attributes in ActiveRecord (attribute, create, update, validation) Hojung 2015.05.24 1638
30 Ruby Style Guide Hojung 2015.02.05 2016
29 Refresh content automatically after some period time on Rails Hojung 2015.01.22 2566
28 Install ruby-filemagic on Mac (brew, libmagic) Hojung 2014.12.03 1812
27 Byte manipulation in Ruby Hojung 2014.12.03 1723
26 How to use Github for your project file Hojung 2014.09.24 1394
25 Ruby map, each, collect, inject, reject, select quick reference Hojung 2014.06.10 2071
24 Difference between collect, select, map and each in ruby Hojung 2014.06.10 1560
23 remove nil from array (compact, compact!) Hojung 2014.06.10 1864
22 remove child association model when parent removed (rails-dependent-destroy) Hojung 2014.06.03 1946
21 deploying cron jobs with whenever gem Hojung 2014.06.01 1975
20 Get list of a class' instance methods Hojung 2014.06.01 1651
19 Optional method parameters in Ruby Hojung 2014.05.29 1762
18 Best Ruby On Rails Content Management Systems (CMS) Hojung 2014.04.26 2945
» The simple way to print exceptions in Ruby Hojung 2014.04.07 1771
16 Ruby Symbols Hojung 2014.04.05 6547
15 What is the difference between a Symbol and String? Hojung 2014.04.05 1737
14 install Rbenv, ruby and rails Hojung 2014.03.22 1955
13 HTTP Posts in Ruby Hojung 2014.03.14 1957
12 optparse: mandatory and optional arguments example Hojung 2014.03.14 2666
Board Pagination ‹ Prev 1 2 Next ›
/ 2

Designed by sketchbooks.co.kr / sketchbook5 board skin

나눔글꼴 설치 안내


이 PC에는 나눔글꼴이 설치되어 있지 않습니다.

이 사이트를 나눔글꼴로 보기 위해서는
나눔글꼴을 설치해야 합니다.

설치 취소

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5