2 Replies - 1057 Views - Last Post: 06 August 2016 - 01:01 AM Rate Topic: -----

#1 AmericanPi  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 14
  • Joined: 30-July 16

How is this Exception working?

Posted 04 August 2016 - 01:07 PM

this is from Derek Banas's YTb Ruby Tut.
need some help understanding.

# You can throw your own exceptions with raise
 

def check_age(age)
  raise ArgumentError, "Enter Positive Number" unless age > 0
end
 
# You catch them with rescue
 
begin
  check_age(-1)
rescue ArgumentError
  puts "That is an impossible age"
end


so...I think the "raise ArgumentError" and then "Enter a Postive Number" is me saying I want to specifically say this if an a argument error is raised, and then i give a condition to define when/how the ArgumentError is triggered.

Right?

but then i have the bit of code on the bottom that i feel means i can uhm, well i don't really know.
i get the jyst of this but i'm confused with "raise" at the same time.

i think the point of exceptions is that i can speciify what i want to be an error and what i want it to say.


Is This A Good Question/Topic? 0
  • +

Replies To: How is this Exception working?

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2511
  • View blog
  • Posts: 3,985
  • Joined: 21-June 11

Re: How is this Exception working?

Posted 05 August 2016 - 07:15 AM

First of all, to make the syntax a bit clearer, what you have here is a method call to the method raise with the arguments ArgumentError and "Enter Positive Number". This method call is inside an unless that uses postfix syntax. unless is simply the opposite form of if and both can be used either postfix after a single expression or prefix with a block. So check_age could equivalently be written like this:

def check_age(age)
  unless age > 0
    raise(ArgumentError, "Enter Positive Number")
  end
  # Or:
  if age <= 0
    raise(ArgumentError, "Enter Positive Number")
  end
end



Now what raise does is to create an exception object that is of the given class and contains the given message and then throw that exception (`raise` can also be used with an already-created exception object rather than a class and/or without a second argument for the message - it can also take a third argument that replaces the stack trace). This exception can then be caught using the begin ... rescue ... end construct. If it is not caught, the exception's type, message and stack trace will be displayed to the user and the execution of the program will be aborted. If it is caught, the code in the rescue-block will be executed and the program will then resume after the end.

The code in the rescue block can access the message or stack trace of the exception by calling the exception object's message and stack_trace methods. In your case it doesn't and displays a custom message instead. So in your code the message of the exception is simply ignored.
Was This Post Helpful? 0
  • +
  • -

#3 AmericanPi  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 14
  • Joined: 30-July 16

Re: How is this Exception working?

Posted 06 August 2016 - 01:01 AM

GOT IT!!
Mostly - Tx for pointing out the syntax silliness (def cleaner && easier to see in an IF statement).
Gonna look up a couple words in the paragraph two.
But now I understand (think I was on the right track).

TX AGN @sepp2k

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1