Skip to main content

IO

rascal-1.0-SNAPSHOT

Synopsis​

An input/output operation caused an error.

Types​

data RuntimeException = IO(str message);

Usage​

import Exception; (only needed when IO is used in catch)

Description​

This error can be generated for many reasons.

First there may be a problem in the location that is used. It maybe that the schemes is not supported. Examples of supported schemes include http, file, home, mvn and project. It can also be the case that the host that occurs in the location cannot be found.

Second, while trying to open the file things can go wrong like insufficient access rights

Finally, actual reading or writing can fail (device failure, device full, and the like).

Remedies:

  • Check for any errors in the location you are using.
  • Check that you are allowed to read or write the resource indicated by the location.
  • Catch IO using a try catch.

Examples​

Import the IO library and attempt to use a non-existing scheme:

rascal>import IO;
ok
rascal>readFile(|myScheme:///example.rsc|);
|mvn://org.rascalmpl--rascal--0.43.0/IO.rsc|(18622,1660,<547,0>,<576,24>): IO("Unsupported scheme \'myscheme\'")
at readFile(|mvn://org.rascalmpl--rascal--0.43.0/IO.rsc|(18622,1660,<547,0>,<576,24>))
at readFile(|mvn://org.rascalmpl--rascal--0.43.0/IO.rsc|(20247,8,<575,84>,<575,92>))

We can catch this IO error. First import the Rascal exceptions (which are also included in Prelude):

rascal>import Exception;
ok
rascal>try
|1 >>>> readFileLines(|myScheme:///example.rsc|);
|2 >>>>catch IO(msg):
|3 >>>> println("This did not work: <msg>");
This did not work: Unsupported scheme 'myscheme'
ok