Resolves #5588: adds openFileStream proc that throws on failure (#7282)

This commit is contained in:
twetzel59 2018-03-04 11:30:28 -05:00 • committed by Andreas Rumpf
commit 9079517d71
2 changed files with 20 additions and 0 deletions

View file

@ -447,9 +447,19 @@ when not defined(js):
## creates a new stream from the file named `filename` with the mode `mode`.
## If the file cannot be opened, nil is returned. See the `system
## <system.html>`_ module for a list of available FileMode enums.
## **This function returns nil in case of failure. To prevent unexpected
## behavior and ensure proper error handling, use openFileStream instead.**
var f: File
if open(f, filename, mode, bufSize): result = newFileStream(f)
proc openFileStream*(filename: string, mode: FileMode = fmRead, bufSize: int = -1): FileStream =
## creates a new stream from the file named `filename` with the mode `mode`.
## If the file cannot be opened, an IO exception is raised.
var f: File
if open(f, filename, mode, bufSize):
return newFileStream(f)
else:
raise newEIO("cannot open file")
when true:
discard

View file

@ -0,0 +1,10 @@
discard """
file: "tstreams3.nim"
output: "threw exception"
"""
import streams
try:
var fs = openFileStream("shouldneverexist.txt")
except IoError:
echo "threw exception"