We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 1fb3e98 commit d46fd68Copy full SHA for d46fd68
chapter_6/exercise_1/myfile.erl
@@ -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