📜  在 Ruby 中引发异常

📅  最后修改于: 2022-05-13 01:54:23.745000             🧑  作者: Mango

在 Ruby 中引发异常

异常是在程序执行期间(即在运行时)发生的不希望或意外事件,它破坏了程序指令的正常流程。正如我们所知,包含在beginend块之间的代码对于处理异常是完全安全的, rescue块告诉 ruby,要处理的异常类型。
主要是处理运行时异常,如果它们是代码中的任何错误,可能是由天真的编码人员编写的,这些类型的错误可能会上升“除以零错误”,“索引超出范围错误”等程序停止执行时如果不加以处理,就会发生这些异常。通过使用 raise 语句,我们可以手动引发用户定义的异常。例如,在 ATM 交易程序中,当用户输入无效的帐号进行提款时会引发异常。
句法:

raise exception-type "exception message" condition

每当引发 raise 语句时,它都会调用救援并从那里开始执行。 raise 语句默认引发 RuntimeError。
例子 :

Ruby
# Ruby program to illustrate 
# use of raise statement
   
begin
          
    puts 'This is Before Exception Arise!'
          
       # using raise to create an exception  
       raise 'Exception Created!'
    
    puts 'After Exception'
end


Ruby
#!/usr/bin/ruby
puts "type-1\n"
begin
 
  # re-raises the current exception
  # (RuntimeError as they are no current exception)
   raise
rescue
   puts 'Tony got rescued.'
end
puts 'Tony returned safely'
  
puts "\ntype-2\n"
begin
 
   # sets this message to the string in the superclass,
   # this exception will be given top priority in the call stack.
   raise 'Quill got rescued.'
   puts 'quill'  # won't execute
rescue StandardError => e 
   puts e.message
end
puts 'Quill is back to ship.'
  
puts "\ntype-3\n"
begin
# uses the first argument to create an exception
# and then sets the message to the second argument.
   raise StandardError.new 'Groot got rescued.'
rescue StandardError => e  # e=>object
 
  # prints the attached string message.
  puts e.message
end
  
puts "\ntype-4\n"
begin
a = 30
b = 0
# here a conditional statement is added
# to execute only if the statement is true
 
    # raises the exception only if the condition is true
    raise  ZeroDivisionError.new 'b should not be 0' if b == 0
    puts a/b
rescue StandardError => e 
   puts e.message 
end
  
puts
begin
a = 30
 
# changing the b value, it passes the raise and executes further
b = 2
 
    # raises the exception only if the condition is true
    raise  ZeroDivisionError.new 'b should not be 0' if b == 0
    print "a/b = ", a / b
rescue StandardError => e 
   puts e.message 
end


Ruby
#!/usr/bin/ruby
puts "Raised Exception:\n"
begin
a = 30
b = 0
 
    # raises the exception only if the condition is true
    raise ZeroDivisionError.new 'b should not be 0' if b == 0
    print "a/b = ", (1 + 2) * (a / b)
rescue ZeroDivisionError => e 
   puts e.message
 
   # prints the error stack, but a raised exception has zero stack
   puts e.backtrace
end
  
puts "\nRuntime Exception:\n"
begin
a = 30
b = 0
x=(1 + 2) * (a / b)
 
    # raises the exception only if the condition is true
    raise ZeroDivisionError.new 'b should not be 0' if b == 0
    print "a/b = ", (1 + 2) * (a / b)
rescue ZeroDivisionError => e 
 
  # prints the message=>(divided by 0)
  # from ZeroDivisionError class
   puts e.message
   puts e.backtrace
end


输出 :

This is Before Exception Arise!
Exception Created!

  • 查看以下代码中的四种 raise 语句:
    例子 :

红宝石

#!/usr/bin/ruby
puts "type-1\n"
begin
 
  # re-raises the current exception
  # (RuntimeError as they are no current exception)
   raise
rescue
   puts 'Tony got rescued.'
end
puts 'Tony returned safely'
  
puts "\ntype-2\n"
begin
 
   # sets this message to the string in the superclass,
   # this exception will be given top priority in the call stack.
   raise 'Quill got rescued.'
   puts 'quill'  # won't execute
rescue StandardError => e 
   puts e.message
end
puts 'Quill is back to ship.'
  
puts "\ntype-3\n"
begin
# uses the first argument to create an exception
# and then sets the message to the second argument.
   raise StandardError.new 'Groot got rescued.'
rescue StandardError => e  # e=>object
 
  # prints the attached string message.
  puts e.message
end
  
puts "\ntype-4\n"
begin
a = 30
b = 0
# here a conditional statement is added
# to execute only if the statement is true
 
    # raises the exception only if the condition is true
    raise  ZeroDivisionError.new 'b should not be 0' if b == 0
    puts a/b
rescue StandardError => e 
   puts e.message 
end
  
puts
begin
a = 30
 
# changing the b value, it passes the raise and executes further
b = 2
 
    # raises the exception only if the condition is true
    raise  ZeroDivisionError.new 'b should not be 0' if b == 0
    print "a/b = ", a / b
rescue StandardError => e 
   puts e.message 
end
输出:
type-1
Tony got rescued.
Tony returned safely

type-2
Quill got rescued.
Quill is back to ship.

type-3
Groot got rescued.

type-4
b should not be 0

a/b = 15

  • 查看运行时和引发异常之间的区别。
    例子 :

红宝石

#!/usr/bin/ruby
puts "Raised Exception:\n"
begin
a = 30
b = 0
 
    # raises the exception only if the condition is true
    raise ZeroDivisionError.new 'b should not be 0' if b == 0
    print "a/b = ", (1 + 2) * (a / b)
rescue ZeroDivisionError => e 
   puts e.message
 
   # prints the error stack, but a raised exception has zero stack
   puts e.backtrace
end
  
puts "\nRuntime Exception:\n"
begin
a = 30
b = 0
x=(1 + 2) * (a / b)
 
    # raises the exception only if the condition is true
    raise ZeroDivisionError.new 'b should not be 0' if b == 0
    print "a/b = ", (1 + 2) * (a / b)
rescue ZeroDivisionError => e 
 
  # prints the message=>(divided by 0)
  # from ZeroDivisionError class
   puts e.message
   puts e.backtrace
end
输出:
Rasied Exception:
b should not be 0
(repl):8:in `'
/run_dir/repl.rb:41:in `eval'
/run_dir/repl.rb:41:in `run'


Runtime Exception:
divided by 0
(repl):21:in `/'
(repl):21:in `'
/run_dir/repl.rb:41:in `eval'
/run_dir/repl.rb:41:in `run'

  • 在上面的例子中,运行时异常有一个错误堆栈