Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualKhatharr

Posted 20 August 2013 - 05:11 PM

Ruby allows this with a simpler syntax.

 

'Normal' exception handling:

begin
  10 / 0
rescue
  puts "Please do not divide by zero."
ensure #the 'ensure' section will always execute when the block exits, regardless of exceptions
  puts "Moving right along..."
end

'Inline' exception handling:

10 / 0 rescue puts "Please do not divide by zero."
puts "Moving right along..."

In the 'inline' case, the handler extends to the end of the line. You can have multiple statements if you use semicolons to separate them.


#2Khatharr

Posted 20 August 2013 - 05:08 PM

Ruby allows this with a simpler syntax.

 

'Normal' exception handling:

begin
  10 / 0
rescue
  puts "Please do not divide by zero."
ensure #the 'ensure' section will always execute when the block exits, regardless of exceptions
  puts "Moving right along..."
end

'Inline' exception handling:

10 / 0 rescue(puts "Please do not divide by zero.")
puts "Moving right along..."

#1Khatharr

Posted 20 August 2013 - 05:07 PM

Ruby allows this with a simpler syntax.

 

'Normal' exception handling:

begin #starts block
  #main code body for the block
rescue ExceptionType #catches exception of indicated type
  #do stuff here
ensure 
  #stuff here always executes when the block exits, even if an exception happened
end #ends block

'Inline' exception handling:

10 / 0 rescue(puts "Please do not divide by zero.")
puts "Moving right along..."

PARTNERS