Forum: Ruby BIG ERROR:class or module required for rescue clause (TypeEr

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3c236efb9841ee697243432ed2d08618?d=identicon&s=25 W0rtex Indigo (w0rtex)
on 2007-06-07 17:14
require 'socket'
require 'timeout'
def ping( host, service = "echo",timeout = 2)
    begin
        timeout( timeout ){
            TCPSocket.open( host, service){}
        }
    rescue Errno::ECONNREFUSED
        true
    rescue false
    else true
    end
end
p ping(ARGV[0] || "google.ru")

When host is down i got error:" ping:10:in `ping': class or module
required for rescue clause (TypeError) from ping:14"
when host is alive i've got true
Can you help me please!
8f6f95c4bd64d5f10dfddfdcd03c19d6?d=identicon&s=25 Rick Denatale (rdenatale)
on 2007-06-07 18:35
(Received via mailing list)
On 6/7/07, W0rtex Indigo <indigo7333@gmail.com> wrote:
>     else true
>     end
> end
> p ping(ARGV[0] || "google.ru")
>
> When host is down i got error:" ping:10:in `ping': class or module
> required for rescue clause (TypeError) from ping:14"
> when host is alive i've got true
> Can you help me please!

It's complaining about
rescue false

Maybe something like:

def ping( host, service = "echo",timeout = 2)
   begin
       timeout( timeout ){
           TCPSocket.open( host, service){}
       }
   rescue Exception => ex
      # handle any expected exceptions
      case ex
        when  Errno::ECONNREFUSED
           return false
        when  Timeout::Error
            return false
        else
            # pass the buck
            raise ex
        end
   end
   true
end

Doing a ping by connecting is rather heavyweight though.

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2007-06-07 18:37
(Received via mailing list)
On 07.06.2007 17:14, W0rtex Indigo wrote:
> require 'socket'
> require 'timeout'
> def ping( host, service = "echo",timeout = 2)
>     begin
>         timeout( timeout ){
>             TCPSocket.open( host, service){}
>         }
>     rescue Errno::ECONNREFUSED
>         true
>     rescue false
              ^^^^^ this is not a class.

Either put a class here or nothing.

>     else true
>     end
> end
> p ping(ARGV[0] || "google.ru")
>
> When host is down i got error:" ping:10:in `ping': class or module
> required for rescue clause (TypeError) from ping:14"
> when host is alive i've got true
> Can you help me please!

The question is, what do you want to achieve?

Kind regards

  robert
3c236efb9841ee697243432ed2d08618?d=identicon&s=25 W0rtex Indigo (w0rtex)
on 2007-06-07 22:18
thank you.))))
This topic is locked and can not be replied to.