Ruby Errors & Exceptions

Source: Internet
Author: User

When your first started coding, errors were probably the last thing you wanted to see.

After all, it's not a far stretch to associate "error" with "I messed up".

Hopefully by now you've come to appreciate the value of a good error message. Take a look at the following errors:

One + 3or'one' for Main:object
One + 3typeerror:no implicit conversion of fixnum into String

Both errors were triggered by the same line of code. Each is a totally different error message, though.

The first, NameError lets you know that's one a variable that hasn ' t been defined. To fix this and you'll have to actually define the variable one .

The second, TypeError lets you know so you ' re trying to add a to Fixnum a String .

Here, there's a good chance that's one a variable that's already set to a String.

Because These errors were meaningful errors, they allowed you to debug your program painlessly, and ideally also provided A learning opportunity.

Inheriting an Exception

In Ruby, just on everything is an object, including errors. Turns out and NameError is TypeError simply the class names for these errors!

All errors inherit their functionality from the Exception class.

There is about twenty or so default errors baked to Ruby (read more about them here) and some indicate more serious ISS UEs than others.

Issues that deal with problems in your code (as opposed to your computer being on fire) all inherit from StandardError . This includes errors like NameError and TypeError .

This means so if you've got a custom error that you ' d like to create, like an InvalidPasswordError , can easily create one by inherit ing from StandardError .

class Invalidpassworderror < standarderrorend

It ' s really that's easy. You don ' t even need anything inside this class!

To manually trigger a exception or error, which can be described as "raising" or "throwing" a exception, use the raise K Eyword.

Raise Invalidpassworderrorinvalidpassworderror:invalidpassworderror

Easy enough. Now you know so can raise this InvalidPasswordError whenever it ' s appropriate.

May I suggest your use of it when a password is ... invalid?

But this still isn ' t super descriptive. Luckily, you can raise a error with an additional message.

Raise " The password entered is invalid. "   is invalid.

That's more like it. Now it's explicit what went wrong if this particular exception is thrown.

means a comprehensive guide to throwing errors and exceptions.

This material could fill a course by itself, and it is a topic we'll return to later on this material.

This was, however, the most common-the-exceptions and errors being thrown in the wild.

Exceptional Errors

When the other developers is using your code, it's a good idea-to-bake meaningful errors right-to-your public API.

Let's see what you might is able to use the the the context of the examples from earlier in the InvalidPasswordError lesson.

classInvalidpassworderror <StandarderrorendclassCustomer attr_reader:fundsdefInitialize (funds, password) @password=Password @funds=funds Enddefwithdraw_securely (amount, password)ifPassword = =@password remove_funds (amount)Else      RaiseInvalidpassworderror,"' #{password} ' is not the correct password."End End Privatedefremove_funds (amount) @funds-=Amount EndEnd

Now, if the correct password is entered, the funds is removed as expected.

But this time, if the incorrect password are entered, your new is InvalidPasswordError thrown with a useful little message.

Kim = customer.new (1000,"Coolpassword")#= = #<customer:0x007faabc8012b8 @password = "Coolpassword", @funds =1000>Kim.withdraw_securely (200,"Coolpassword")# +/-Kim.withdraw_securely (150,"Badpassword") Invalidpassworderror:'Badpassword'  is  notThe correct password.

That ' s so useful!

Ruby Errors & Exceptions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.