Skip to content

Commit d46fd68

Browse files
committed
Complete exercise 6.1.
1 parent 1fb3e98 commit d46fd68

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

chapter_6/exercise_1/myfile.erl

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
-module(myfile).
2+
3+
-export([read/1]).
4+
5+
% myfile:read returns the contents of the file at
6+
% the specified path or raise an exception if the file
7+
% doesn't exist or an error occurs while reading.
8+
%
9+
% While the exercise doesn't specify it, I am assuming
10+
% that we want to control the exception that is raised
11+
% so I am using a case statement.
12+
read(File) ->
13+
case file:read_file(File) of
14+
{ok, Bin} ->
15+
% Read was successful, return contents of file.
16+
Bin;
17+
{error, Why} ->
18+
% Raise an exception with `{failed, Why}` reason.
19+
throw({failed, Why})
20+
end.

0 commit comments

Comments
 (0)