2009-10-09 1 views

답변

189

하는 방법은 "데프"A "시작"문 역할을 할 수 있습니다 :

def foo 
    ... 
rescue 
    ... 
end 
+0

덕분에 나는 단지 어떻게 작동하는지 알고 싶었습니다. 그게 내가 찾던 것들 이었어. 고맙습니다. – Sid

+2

또한, 클래스 정의, 모듈 정의 그리고'do' /'end' 블록 리터럴은 암시적인 예외 블록을 형성합니다. –

+0

당신은 구출을 보장 할 수 있습니까? –

41

또한 인라인 구출 할 수 있습니다 : "예외"

1 + "str" rescue "EXCEPTION!" 

가 인쇄됩니다 나는 DEF/구조 조합 액티브 검증에 많이 사용하고

+1

어떻게 구출하고 예외 백 트레이스 인라인을 표시합니까? –

+0

실제 예외를 반환하는 방법은 무엇입니까? – user1735921

21

'문자열 Fixnum이라는 강요 할 수 없다'이후 :

def create 
    @person = Person.new(params[:person]) 
    @person.save! 
    redirect_to @person 
rescue ActiveRecord::RecordInvalid 
    render :action => :new 
end 

을 나는 이것이 매우 린 코드라고 생각합니다!

14

예 : begin 문으로

begin 
    # something which might raise an exception 
rescue SomeExceptionClass => some_variable 
    # code that deals with some exception 
ensure 
    # ensure that this code always runs 
end 
여기

, def :

def 
    # something which might raise an exception 
rescue SomeExceptionClass => some_variable 
    # code that deals with some exception 
ensure 
    # ensure that this code always runs 
end 
관련 문제