際際滷
Submit Search
Ruby exceptions
1 like
734 views
Andrei Kaleshka
Follow
1 of 27
Download now
Download to read offline
More Related Content
Ruby exceptions
1.
从仍ム亠仆亳 亳 Ruby 仆亟亠亶
仂仍亠从仂 @ka8725 ka8725@gmail.com
2.
弍亳亠 仗仂弍仍亠仄 仗仂亟仂亟舒
从 舒亰舒弍仂从亠 仂亞舒仄仄亳亶亠 仆亠 仆舒 磶从亠, 舒 仗仂仄仂 磶从舒. 弌. 舒从从仂仆仆亠仍仍
3.
仂弍仍亠仄 亳从仍ム亠仆亳亶 仗仂仍亰仂于舒仆亳亠 亳从仍ム亠仆亳亶
于 仆亠亳从仍ム亳亠仍仆 亳舒亳
4.
亳仄亠 def create @user =
User.new params[:user] @user.save! redirect_to user_path(@user) rescue ActiveRecord::RecordNotSaved flash[:notice] = 'Unable to create user' render :action => :new end
5.
舒于亳仍仆亶 仗仂亟仂亟 def create @user
= User.new params[:user] if @user.save redirect_to user_path(@user) else flash[:notice] = 'Unable to create user' render :action => :new end end
6.
亟亠 亳仗仂仍亰仂于舒 仄亠仂亟, 亞亠仆亠亳ム亳亠
亳从仍ム亠仆亳 仂亞 弍 仗仂仍亠亰仆 于 亠舒 亟仍 仗仂于亠从亳 于舒仍亳亟舒亳亶 舒仆亰舒从亳 亟仍 仄仍亳亳 仂从舒舒
7.
亟亠 仆亠 亳仗仂仍亰仂于舒
仄亠仂亟, 亞亠仆亠亳ム亳亠 亳从仍ム亠仆亳 仂 于仂亰仄仂亢仆仂亳 于亠亰亟亠! 丐舒从 亳仍亳 亳仆舒亠, 仆舒仄 仆亠 亳亰弍舒于亳 仂 仍舒亠于, 从仂亞亟舒 弍亠亰 亳从仍ム亠仆亳亶 仆亠 仂弍仂亶亳
8.
舒 仂 仄
仍ミ頴湖 Ruby? File.open('testfile') do |file| while line = file.readline puts line end rescue EOFError return end ミ頴湖姉 弍 仄 Ruby 亰舒 仂?
9.
舒 仂 仄
仍ミ頴湖 Ruby? File.open('testfile') do |file| while line = file.gets puts line end end 仍ミ頴湖 Ruby 亰舒 仂:
10.
亳仄亠 亳从仍ム亳亠仍仆 亳舒亳亶 仂亠
仂亠亟亳仆亠仆亳 弍舒亰仂亶 亟舒仆仆 亠亠仗仂仍仆亠仆亳亠 仗舒仄亳 亳弍从舒 亠仆亳/亰舒仗亳亳 仂从亠舒 弌仍舒亳, 于仂亰仆亳从舒ム亳亠 于仍亠亟于亳亳 仗仂仍亰仂于舒亠仍从仂亞仂 于于仂亟舒 磦仍ム 亳从仍ム亳亠仍仆仄亳 亳舒亳礆亳!
11.
仂仍亢仆 仍亳 仄
仂仍舒于仍亳于舒 亳从仍ム亳亠仍仆亠 亳舒亳亳? 亠 亰舒于亳亳 仂 亳仗舒 仗亳仍仂亢亠仆亳 亳 亠亞仂 亠弍仂于舒仆亳亶 弍仂仍亳仆于亠 仍舒亠于 仆亠 ミ頴委 仂从舒 于 从仂亟亠 仄仂亢亠 于亰于舒 亳从仍ム亠仆亳亠. 仍舒于仍亳于舒 于亠?
12.
亠 仍仂于亳亠 于亠 user.address.street
rescue
13.
亠 仍仂于亳亠 于亠 user.address.street
rescue # 舒 从舒从 仆舒亠 仂亞仂: user.address.bla_bla_bla rescue
14.
亟亠 于舒亰亳亠仍仆亠亠 if user.address user.address.street end
15.
亟亠 仄仂亢亠 弍
仗仂仍亠亰亠仆 inline rescue? val_or_error = {}.fetch(:name) rescue $! val_or_error #=> #<KeyError: key not found: :name> 仂于亳仄 亳从仍ム亠仆亳亠 亳 舒仆舒仍亳亰亳亠仄 亠亞仂:
16.
亠 仗亠亠于舒于舒亶亠 Exception class TaskProcessor def
perform(id) task = Task.find(id) task.process rescue Exception => e task.log(e.message) raise end end
18.
亠亰仍舒 NoMethodError: unde鍖ned method
`log' for nil:NilClass 亠亶于亳亠仍仆亶 亢亳亟舒亠仄亶 ActiveRecord::RecordNotFound: Couldn't 鍖nd Task with id=1000
19.
个亳从 class TaskProcessor def perform(id) task
= Task.find(id) task.process rescue ProcessError => e task.log(e.message) raise end end
21.
亳 仂弍仂亟舒 亠亟仂舒于仍磺亠
舒亠亞亳 仂仗仍亠仆亳 亳仄亠仆磺亠 仗舒亠仆 仗仂亠从亳仂于舒仆亳 亳亳亠 亠 弌仍仂亢仆仂 仆舒仗亳舒仆亳 亠舒 从舒亢亠 仆舒 仗仍仂仂 仗仂亠从亳仂于舒仆仆 亳亠仄
22.
Hash#fetch h = {} h.fetch(:name)
# =>KeyError: key not found: :name h.fetch(:name) { 'ka8725' } # => "ka8725"
23.
弍舒仆舒 于磶 def run on_success
do # 仂弍舒弍仂从舒 仗亠舒 end on_failure do # 仂弍舒弍仂从舒 仂亳弍从亳 end end http://goo.gl/lchi1K
24.
Throw/catch def invoke @res =
catch(:halt) { yield } # 亟舒仍亠 舒仆舒仍亳亰 res end invoke { throw :halt, "response 1"} @res #=> "response 1" invoke { "response 2" } @res #=> "response 2" http://goo.gl/qey41F - 亳仂亟仆亳从亳 sinatra
25.
舒于亳仍仂 于弍舒于舒仆亳 亳从仍ム亠仆亳亶 亠亶于 丐仂仄舒
亳 亅仆亟亳 丱舒仆, 舒亞仄舒亳从-仗仂亞舒仄仄亳
26.
亳亠舒舒 http://exceptionalruby.com/ http://eloquentruby.com/ http://pragprog.com/the- pragmatic-programmer http://cc2e.com/
27.
仂仗仂? 仆亟亠亶 仂仍亠从仂 @ka8725 ka8725@gmail.com
Download