0

Exception IO has structure:

Exception IO of {
name: string
....
...}

some other arguments that I do not understand.

Do I have to assign all these. I mean what do I do after this?

exception IO of {inputfile}

I usually define exception and then raise. but I do not even define an exception this way.

All I want to do is raise an exception if input file is not existant. What do I do here?

Thank You

700resu
  • 259
  • 6
  • 16

1 Answers1

1

Yes, you have to supply all three fields when creating an exception of type Io. The meanings of the fields are explained in the documentation:

This is the principal exception raised when an error occurs in the I/O subsystem. The components of Io are:

  • name: The name component of the reader or writer.
  • function: The name of the function raising the exception.
  • cause: The underlying exception raised by the reader or writer, or detected at the stream I/O level.

Some of the standard causes are:

  • OS.SysErr if an actual system call was done and failed. *Subscript if ill-formed arguments are given.
  • BlockingNotSupported
  • NonblockingNotSupported
  • ClosedStream

The cause field of Io is not limited to these particular exceptions. Users who create their own readers or writers may raise any exception they like, which will be reported as the cause field of the resulting Io exception.

Note that openIn already raises an Io exception if the file does not exist (with "openIn" as the function, the filename as the name and a SysErr as the cause), so there's no need for you to raise your own.

sepp2k
  • 363,768
  • 54
  • 674
  • 675
  • Suppose, I have to define my own exception. where do I raise the exception? I define exception at the beginning and then raise it when error occurs. How do I handle here? for example TextIO.openIn("something") is function where I cannot define conditions like I do in my own function. where do I raise exceptions here? – 700resu Oct 20 '12 at 21:09
  • @user1710036 Not sure what you mean, you raise the exception where the error occurs. – sepp2k Oct 20 '12 at 21:15
  • I mean in normal cases what I do is: if a then this else raise exception or case sth of a=>.... |b=>.... |_=>raise exceptions. In the case of TextIO.openIn(...) I do not have options to define cases. Where, then, do I define exceptions – 700resu Oct 21 '12 at 14:09
  • @user1710036 I still don't understand the question. If you're using `openIn`, you don't have to raise an exception if the file is not found - `openIn` already raises that exception itself. – sepp2k Oct 21 '12 at 15:11
  • Sorry. I myself understood the problem now. I have to handle the Io and return [] if file is not found. I wrote handle Io=>[] after val ins=TextIO.openIn(...) but did not work. My question is where do I put it to handle it. Sorry for being unclear. I am beginner. – 700resu Oct 22 '12 at 15:25
  • @user1710036 Define "did not work". One problem I see is that you used `Io` without an argument. `Io` takes one argument, so you need to use a pattern with one argument. If you don't care about the details of the exception, just use `Io _`. Another problem is that you're returning a list in your handle clause, but `openIn` doesn't return a list - it returns a handle. So that's a type error. If you want to return a list, you need to wrap the `handle` around a larger expression that returns a list. – sepp2k Oct 22 '12 at 15:31
  • Did not work means: It raises exception IO but does not handle it or return [ ]. what I did is this: I wrote handle Io_after your suggestion and I have it wrapped like this: fun mextract(infile:string) = let val ins = TextIO.openIn(infile) fun extractline_h (ins) = case TextIO.inputLine(ins) of NONE=>[] |SOME(l)=> numfrmstr(l)::extractline_h(ins) handle Overflow=>extractline_h(ins) in extractline_h(ins) handle Io_=>[] before TextIO.closeIn ins end ; – 700resu Oct 22 '12 at 15:42
  • I am suggested to refrain from using "Your Answer" below for clarification and the formatting looks awkward here. Sorry for that. – 700resu Oct 22 '12 at 15:45
  • 1
    @user1710036 You should wrap the `handle` around the whole let-expression. – sepp2k Oct 22 '12 at 15:54
  • Thanks. Thats one place that I had not tried. You have no idea how much of my time you saved. Tonnes of Thanks. – 700resu Oct 22 '12 at 15:57