Ruby Error Handling and exceptions

Source: Internet
Author: User

I. Common anomalies
Exception name Common causes How to Throw
RuntimeError Default exception thrown by raise Raise
Nomethoderror Object cannot find a corresponding method A=oject.new
A.jackmethod
Nameerror The interpreter encounters an identifier that cannot be resolved to a variable or method name A=jack
IOError Reads a closed stream, writes a read-only stream, or a similar operation Stdin.puts ("Cannot write")
Errno::error A class of errors related to file IO File.Open (-10)
TypeError Method accepts a parameter that it cannot handle a=3+ "ABC"
Argumenterror Error passing the number of parameters def o (x) End O (All-you-do)
Second, catch the exceptioncatching exceptions with rescue#用rescue捕获异常beginResult=20/0puts resultRescueZerodivisionerrorputs "Zero Error"RescuePuts "Unknow error"EndOutput: Zero Error three,raise throws an exceptiondef divide (x) Raise Argumenterror if X==0end begindivide (0) Rescue argumenterrorputs "Argumenterror" End output: Argumenterror Iv. Save the exception to the variable def divide (x) Raise Argumenterror if X==0end begindivide (0)Rescue =>ePuts E.to_send output: Argumenterror v. Create exception classthrowexceptionl<exceptionPuts "error L" End Beginraise throwexceptionl, "Got error" rescue throwexceptionl=>eputs "error #{e}" End output: Error lerror Got error

Sample Ruby exception handling structure code:

    1. Begin #开始
    2. Raise. #抛出异常
    3. Rescue [Exceptiontype =
      Standardexception]
      #捕获指定类型的异常 Default value is Standardexception
    4. $! #表示异常信息
    5. [Email protected] #表示异常出现的代码位置
    6. else #其余异常
    7. ..
    8. Ensure #不管有没有异常, enter the code block
    9. End #结束

Can be combined with $! error causes, and [email protected] Error location to do a bug capture and prompt the small program, such as:

    1. Begin
    2. Puts
    3. Puts "file: #{name = argv.shift}"
    4. File = open (name)
    5. i = 0
    6. File.read.each_line
      {|line| puts "#{i+=1}.#{line}"}
    7. Rescue
    8. Puts "error:#{$!} At:#{[email protected]} "
    9. Ensure
    10. File.close
    11. End

Ruby Error Handling and 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.