Rescu all errors and then case statement

How can I read what type of error occured in rescue clause.
For example, I have the following:

begin
  "some code here"

rescue
  case
   when "error type 1"

   when "error type 2"
  end

end

I know I could raise errors within code section and then have specif
rescues, but I want to have one rescue and then do different things with
it in the rescue.

Thanks in advance.

···

--
Posted via http://www.ruby-forum.com/.

You can do something like this:

begin
...
rescue => e
<do whatever you want with e here>
end

basically, the "=> e" bit will store the thrown error in the "e" variable.

···

On Jan 10, 2008 2:17 PM, Michal Sza <nicpon@nicpon.net> wrote:

How can I read what type of error occured in rescue clause.
For example, I have the following:

begin
  "some code here"

rescue
  case
   when "error type 1"

   when "error type 2"
  end

end

I know I could raise errors within code section and then have specif
rescues, but I want to have one rescue and then do different things with
it in the rescue.

Thanks in advance.

--
Bira

http://sinfoniaferida.blogspot.com

errors = [IOError, ZeroDivisionError, ArgumentError]

errors.each do |klass|
  begin
    puts "raising #{klass.name}"
    raise klass
  rescue => e
    case e
    when IOError
      puts "it's IOError!"
    when ZeroDivisionError
      puts "it's ZeroDivisionError"
    else
      puts "other error: #{e.class.name}"
    end
  end
end

···

On Jan 10, 2008 5:17 PM, Michal Sza <nicpon@nicpon.net> wrote:

How can I read what type of error occured in rescue clause.
For example, I have the following:

begin
  "some code here"

rescue
  case
   when "error type 1"

   when "error type 2"
  end

end

I know I could raise errors within code section and then have specif
rescues, but I want to have one rescue and then do different things with
it in the rescue.

--
Radosław Bułat

http://radarek.jogger.pl - mój blog

that works good. In addition to that is there a way to tell what error
type is it ?

···

--
Posted via http://www.ruby-forum.com/.

e.class

Dan

···

On 1/10/08, Michal Sza <nicpon@nicpon.net> wrote:

that works good. In addition to that is there a way to tell what error
type is it ?
--
Posted via http://www.ruby-forum.com/\.

Daniel Finnie wrote:

e.class

Dan

Thanks guys.

···

--
Posted via http://www.ruby-forum.com/\.