Exceptions list - Unix ENOENT not the name of the exception - what is?

  • Thread starter Graham Nicholls
  • Start date
G

Graham Nicholls

Hi,
I'm trying to open a file, and if the user hasn't given an extension, adding
one, but I can't find out what exception to use. If I leave off the rescue
statements, then I see ENOENT, so try using that, but get a NameError -
ENOENT is the Unix error, not Ruby's. So, what is the exception for a
missing file on IO.foreach, and how in general can I get a list of
exceptions or find out which is the right one to use?

eg:


if $include_header
orig_hdr_fname=header_fname
begin
IO.foreach(header_fname) do |hdr_line|
# Allow comments in header files:
opfile.write(hdr_line) if hdr_line !~ /^\s*#/
end
rescue IOError <---- THIS IS THE PROBLEM
header_fname=header_fname+"frt"
printf("Error reading header file %s - trying
%s\n",orig_hdr_fname,header_fname)
retry
rescue => err
printf("Error opening header file %s\n",err)
exit(BAD_FILE)
end
Thanks
 
A

Austin Ziegler

I'm trying to open a file, and if the user hasn't given an extension, adding
one, but I can't find out what exception to use. If I leave off the rescue
statements, then I see ENOENT, so try using that, but get a NameError -
ENOENT is the Unix error, not Ruby's. So, what is the exception for a
missing file on IO.foreach, and how in general can I get a list of
exceptions or find out which is the right one to use?

Try rescuing Errno::ENOENT.

-austin
 
D

Dick Davies

Out of interest, is there anyway of knowing what exceptions a given
class can raise?

I'd expect not, but I've been wrong before (as any regulars on this list
will know).
 
A

Austin Ziegler

Out of interest, is there anyway of knowing what exceptions a given
class can raise?

I'd expect not, but I've been wrong before (as any regulars on this list
will know).

As far as I know, no. However, I have also read that the "promised"
list of exceptions (cf Java) is more problematic than useful (ISTR
something on Joel on Software about it). However, using the classtree
method (http://www.rubygarden.org/ruby?ASCIIClassHierarchyGenerator),
you can call:

class_tree(Exception, false, false)

And get a tree of possible exceptions. That's where I found Errno::ENOENT.

-austin
 
R

Robert Klemme

Austin Ziegler said:
As far as I know, no. However, I have also read that the "promised"
list of exceptions (cf Java) is more problematic than useful (ISTR
something on Joel on Software about it). However, using the classtree
method (http://www.rubygarden.org/ruby?ASCIIClassHierarchyGenerator),
you can call:

class_tree(Exception, false, false)

And get a tree of possible exceptions. That's where I found
Errno::ENOENT.

In this case catching Exception should have helped, too. Or am I missing
something?

begin
File.open("not possibly found here")
rescue Exception => e
p e
puts e.class
end

Yields this in IRB:

irb(main):001:0> begin
irb(main):002:1* File.open("not possibly found here")
irb(main):003:1> rescue Exception => e
irb(main):004:1> p e
irb(main):005:1> puts e.class
irb(main):006:1> end
#<Errno::ENOENT: No such file or directory - not possibly found here>
Errno::ENOENT
=> nil

Regards

robert
 
A

Ara.T.Howard

Out of interest, is there anyway of knowing what exceptions a given
class can raise?

I'd expect not, but I've been wrong before (as any regulars on this list
will know).

~/build/ruby > grep 'VALUE rb_e' *c
error.c:VALUE rb_eException;
error.c:VALUE rb_eSystemExit;
error.c:VALUE rb_eInterrupt;
error.c:VALUE rb_eSignal;
error.c:VALUE rb_eFatal;
error.c:VALUE rb_eStandardError;
error.c:VALUE rb_eRuntimeError;
error.c:VALUE rb_eTypeError;
error.c:VALUE rb_eArgError;
error.c:VALUE rb_eIndexError;
error.c:VALUE rb_eRangeError;
error.c:VALUE rb_eNameError;
error.c:VALUE rb_eNoMethodError;
error.c:VALUE rb_eSecurityError;
error.c:VALUE rb_eNotImpError;
error.c:VALUE rb_eNoMemError;
error.c:VALUE rb_eScriptError;
error.c:VALUE rb_eSyntaxError;
error.c:VALUE rb_eLoadError;
error.c:VALUE rb_eSystemCallError;
eval.c:static VALUE rb_eLocalJumpError;
eval.c:static VALUE rb_eSysStackError;
eval.c:static VALUE rb_eval _((VALUE,NODE*));
eval.c:static VALUE rb_eThreadError;
io.c:VALUE rb_eEOFError;
io.c:VALUE rb_eIOError;
numeric.c:VALUE rb_eZeroDivError;
numeric.c:VALUE rb_eFloatDomainError;
re.c:static VALUE rb_eRegexpError;

these are most of the errors, all the Errno::xxx errors are made by
SystemCallError:

~ > cat syserr.rb
255.times do |errno|
se = SystemCallError.new(errno)
next if se.message[%r/unknown error/io]
printf "%-3.3d : %s\n", errno, se.inspect
end

~ > ruby syserr.rb
000 : #<SystemCallError: Success>
001 : #<Errno::EPERM: Operation not permitted>
002 : #<Errno::ENOENT: No such file or directory>
003 : #<Errno::ESRCH: No such process>
004 : #<Errno::EINTR: Interrupted system call>
005 : #<Errno::EIO: Input/output error>
006 : #<Errno::ENXIO: No such device or address>
007 : #<Errno::E2BIG: Argument list too long>
008 : #<Errno::ENOEXEC: Exec format error>
009 : #<Errno::EBADF: Bad file descriptor>
010 : #<Errno::ECHILD: No child processes>
011 : #<Errno::EAGAIN: Resource temporarily unavailable>
012 : #<Errno::ENOMEM: Cannot allocate memory>
013 : #<Errno::EACCES: Permission denied>
014 : #<Errno::EFAULT: Bad address>
015 : #<Errno::ENOTBLK: Block device required>
016 : #<Errno::EBUSY: Device or resource busy>
017 : #<Errno::EEXIST: File exists>
018 : #<Errno::EXDEV: Invalid cross-device link>
019 : #<Errno::ENODEV: No such device>
020 : #<Errno::ENOTDIR: Not a directory>
021 : #<Errno::EISDIR: Is a directory>
022 : #<Errno::EINVAL: Invalid argument>
023 : #<Errno::ENFILE: Too many open files in system>
024 : #<Errno::EMFILE: Too many open files>
025 : #<Errno::ENOTTY: Inappropriate ioctl for device>
026 : #<Errno::ETXTBSY: Text file busy>
027 : #<Errno::EFBIG: File too large>
028 : #<Errno::ENOSPC: No space left on device>
029 : #<Errno::ESPIPE: Illegal seek>
030 : #<Errno::EROFS: Read-only file system>
031 : #<Errno::EMLINK: Too many links>
032 : #<Errno::EPIPE: Broken pipe>
033 : #<Errno::EDOM: Numerical argument out of domain>
034 : #<Errno::ERANGE: Numerical result out of range>
035 : #<Errno::EDEADLK: Resource deadlock avoided>
036 : #<Errno::ENAMETOOLONG: File name too long>
037 : #<Errno::ENOLCK: No locks available>
038 : #<Errno::ENOSYS: Function not implemented>
039 : #<Errno::ENOTEMPTY: Directory not empty>
040 : #<Errno::ELOOP: Too many levels of symbolic links>
042 : #<Errno::ENOMSG: No message of desired type>
043 : #<Errno::EIDRM: Identifier removed>
044 : #<Errno::ECHRNG: Channel number out of range>
045 : #<Errno::EL2NSYNC: Level 2 not synchronized>
046 : #<Errno::EL3HLT: Level 3 halted>
047 : #<Errno::EL3RST: Level 3 reset>
048 : #<Errno::ELNRNG: Link number out of range>
049 : #<Errno::EUNATCH: Protocol driver not attached>
050 : #<Errno::ENOCSI: No CSI structure available>
051 : #<Errno::EL2HLT: Level 2 halted>
052 : #<Errno::EBADE: Invalid exchange>
053 : #<Errno::EBADR: Invalid request descriptor>
054 : #<Errno::EXFULL: Exchange full>
055 : #<Errno::ENOANO: No anode>
056 : #<Errno::EBADRQC: Invalid request code>
057 : #<Errno::EBADSLT: Invalid slot>
059 : #<Errno::EBFONT: Bad font file format>
060 : #<Errno::ENOSTR: Device not a stream>
061 : #<Errno::ENODATA: No data available>
062 : #<Errno::ETIME: Timer expired>
063 : #<Errno::ENOSR: Out of streams resources>
064 : #<Errno::ENONET: Machine is not on the network>
065 : #<Errno::ENOPKG: Package not installed>
066 : #<Errno::EREMOTE: Object is remote>
067 : #<Errno::ENOLINK: Link has been severed>
068 : #<Errno::EADV: Advertise error>
069 : #<Errno::ESRMNT: Srmount error>
070 : #<Errno::ECOMM: Communication error on send>
071 : #<Errno::EPROTO: Protocol error>
072 : #<Errno::EMULTIHOP: Multihop attempted>
073 : #<Errno::EDOTDOT: RFS specific error>
074 : #<Errno::EBADMSG: Bad message>
075 : #<Errno::EOVERFLOW: Value too large for defined data type>
076 : #<Errno::ENOTUNIQ: Name not unique on network>
077 : #<Errno::EBADFD: File descriptor in bad state>
078 : #<Errno::EREMCHG: Remote address changed>
079 : #<Errno::ELIBACC: Can not access a needed shared library>
080 : #<Errno::ELIBBAD: Accessing a corrupted shared library>
081 : #<Errno::ELIBSCN: .lib section in a.out corrupted>
082 : #<Errno::ELIBMAX: Attempting to link in too many shared libraries>
083 : #<Errno::ELIBEXEC: Cannot exec a shared library directly>
084 : #<Errno::EILSEQ: Invalid or incomplete multibyte or wide character>
085 : #<Errno::ERESTART: Interrupted system call should be restarted>
086 : #<Errno::ESTRPIPE: Streams pipe error>
087 : #<Errno::EUSERS: Too many users>
088 : #<Errno::ENOTSOCK: Socket operation on non-socket>
089 : #<Errno::EDESTADDRREQ: Destination address required>
090 : #<Errno::EMSGSIZE: Message too long>
091 : #<Errno::EPROTOTYPE: Protocol wrong type for socket>
092 : #<Errno::ENOPROTOOPT: Protocol not available>
093 : #<Errno::EPROTONOSUPPORT: Protocol not supported>
094 : #<Errno::ESOCKTNOSUPPORT: Socket type not supported>
095 : #<Errno::EOPNOTSUPP: Operation not supported>
096 : #<Errno::EPFNOSUPPORT: Protocol family not supported>
097 : #<Errno::EAFNOSUPPORT: Address family not supported by protocol>
098 : #<Errno::EADDRINUSE: Address already in use>
099 : #<Errno::EADDRNOTAVAIL: Cannot assign requested address>
100 : #<Errno::ENETDOWN: Network is down>
101 : #<Errno::ENETUNREACH: Network is unreachable>
102 : #<Errno::ENETRESET: Network dropped connection on reset>
103 : #<Errno::ECONNABORTED: Software caused connection abort>
104 : #<Errno::ECONNRESET: Connection reset by peer>
105 : #<Errno::ENOBUFS: No buffer space available>
106 : #<Errno::EISCONN: Transport endpoint is already connected>
107 : #<Errno::ENOTCONN: Transport endpoint is not connected>
108 : #<Errno::ESHUTDOWN: Cannot send after transport endpoint shutdown>
109 : #<Errno::ETOOMANYREFS: Too many references: cannot splice>
110 : #<Errno::ETIMEDOUT: Connection timed out>
111 : #<Errno::ECONNREFUSED: Connection refused>
112 : #<Errno::EHOSTDOWN: Host is down>
113 : #<Errno::EHOSTUNREACH: No route to host>
114 : #<Errno::EALREADY: Operation already in progress>
115 : #<Errno::EINPROGRESS: Operation now in progress>
116 : #<Errno::ESTALE: Stale NFS file handle>
117 : #<Errno::EUCLEAN: Structure needs cleaning>
118 : #<Errno::ENOTNAM: Not a XENIX named type file>
119 : #<Errno::ENAVAIL: No XENIX semaphores available>
120 : #<Errno::EISNAM: Is a named type file>
121 : #<Errno::EREMOTEIO: Remote I/O error>
122 : #<Errno::EDQUOT: Disk quota exceeded>
123 : #<SystemCallError: No medium found>
124 : #<SystemCallError: Wrong medium type>
125 : #<SystemCallError: Operation canceled>


cheers.

-a
--
===============================================================================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| A flower falls, even though we love it;
| and a weed grows, even though we do not love it.
| --Dogen
===============================================================================
 
L

Lennon Day-Reynolds

In this case catching Exception should have helped, too. Or am I missing
something?

begin
File.open("not possibly found here")
rescue Exception => e
p e
puts e.class
end

This will work, but catch-all rescue blocks like that can be
dangerous, as they can mask user interrupts, missing method errors,
etc. Using the most specific exception class that still covers the
errors you expect to see in a block means that unexpected errors will
be thrown up to higher levels on the stack, which is exactly what
should happen.

Lennon
 
R

Robert Klemme

Lennon Day-Reynolds said:
[...]
In this case catching Exception should have helped, too. Or am I missing
something?

begin
File.open("not possibly found here")
rescue Exception => e
p e
puts e.class
end

This will work, but catch-all rescue blocks like that can be
dangerous, as they can mask user interrupts, missing method errors,
etc. Using the most specific exception class that still covers the
errors you expect to see in a block means that unexpected errors will
be thrown up to higher levels on the stack, which is exactly what
should happen.

Of course you are right, but this code snippet was just meant to determine
the proper type. I was responding to another suggestion how to find the
type of this exception. This was not meant to be program code.

Regards

robert
 
Y

YANAGAWA Kazuhisa

In Message-Id: <[email protected]>
Robert Klemme said:
Of course you are right, but this code snippet was just meant to determine
the proper type. I was responding to another suggestion how to find the
type of this exception. This was not meant to be program code.

Actually no extra code is needed in this case:
ruby -e 'File.open("no_such_file")'
-e:1:in `initialize': No such file or directory - no_such_file (Errno::ENOENT)
from -e:1:in `open'
from -e:1

As you can see, a class of an exception raised is showed in () of the
message.

Just FYI.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top