Skip to content

Commit 82c5ac0

Browse files
author
BennyIjeoma
committed
Feat: Added examples to the exception for a clearer error handlind
1 parent adcb7c6 commit 82c5ac0

File tree

1 file changed

+177
-0
lines changed

1 file changed

+177
-0
lines changed

lib/elixir/lib/exception.ex

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,16 @@ defmodule ArgumentError do
10451045
`ArgumentError` exceptions have a single field, `:message` (a `t:String.t/0`),
10461046
which is public and can be accessed freely when reading or creating `ArgumentError`
10471047
exceptions.
1048+
For example, this exception is when a function expects an integer as an index to access elements in a list but instead receives a string, it might raise an ArgumentError to indicate invalid input.
1049+
iex> :erlang.list_to_integer('abc')
1050+
1051+
1052+
** (ArgumentError) errors were found at the given arguments:
1053+
1054+
* 1st argument: not a textual representation of an integer
1055+
1056+
(erts 15.0) erlang.erl:12782: :erlang.list_to_integer(~c"abc")
1057+
10481058
"""
10491059

10501060
defexception message: "argument error"
@@ -1105,6 +1115,10 @@ defmodule SystemLimitError do
11051115
An exception raised when a system limit has been reached.
11061116
11071117
For example, this can happen if you try to create an atom that is too large.
1118+
1119+
iex> :erlang.binary_to_atom(:erlang.list_to_binary(List.duplicate(100_000, 1)), :utf8)
1120+
** (SystemLimitError) a system limit has been reached
1121+
11081122
"""
11091123

11101124
defexception message: "a system limit has been reached"
@@ -1198,6 +1212,17 @@ defmodule SyntaxError do
11981212
* `:column` - the column where the error occurred
11991213
* `:description` - a description of the syntax error
12001214
1215+
if true else
1216+
IO.puts("This will break")
1217+
end
1218+
1219+
** (SyntaxError) invalid syntax found on Desktop/important/notebook/error_handling.livemd#cell:sslzfkto2taubx37:3:1:
1220+
error: unexpected reserved word: end
1221+
1222+
3 │ end
1223+
1224+
1225+
12011226
"""
12021227

12031228
defexception [:file, :line, :column, :snippet, description: "syntax error"]
@@ -1252,6 +1277,16 @@ defmodule TokenMissingError do
12521277
* `:opening_delimiter` - an atom representing the opening delimiter
12531278
* `:expected_delimiter` - an atom representing the expected delimiter
12541279
* `:description` - a description of the missing token error
1280+
For example, trying to inspect a code and didn't add the end parenthesis:
1281+
iex> IO.puts("Hello, world!"
1282+
** (TokenMissingError) token missing
1283+
error: missing terminator: )
1284+
1285+
1 │ IO.puts("Hello, world!"
1286+
│ │ └ missing closing delimiter (expected ")")
1287+
│ └ unclosed delimiter
1288+
1289+
12551290
"""
12561291

12571292
defexception [
@@ -1337,6 +1372,17 @@ defmodule CompileError do
13371372
the error occurred in code that did not come from a file
13381373
* `:line` (`t:non_neg_integer/0`) - the line where the error occurred
13391374
1375+
For example, this exception gets raised when the function calls an unkwon fucntion:
1376+
iex>defmodule MyApp do
1377+
def greet(name) do
1378+
IO.puts(say_hello(name))
1379+
end
1380+
1381+
MyApp.greet("ijeoma")
1382+
** (CompileError) cannot compile module MyApp (errors have been logged)
1383+
(elixir 1.17.2) src/elixir_module.erl:186: anonymous fn/9 in :elixir_module.compile/7
1384+
1385+
13401386
"""
13411387

13421388
defexception [:file, :line, description: "compile error"]
@@ -1360,6 +1406,19 @@ defmodule Kernel.TypespecError do
13601406
the error occurred in code that did not come from a file
13611407
* `:line` (`t:non_neg_integer/0`) - the line where the error occurred
13621408
1409+
For example,this error is caused because of an exception in the typespec
1410+
iex > defmodule Example do
1411+
@type my_type :: strng()
1412+
end
1413+
1414+
** (Kernel.TypespecError) type strng/0 undefined (no such type in Example)
1415+
(elixir 1.17.2) lib/kernel/typespec.ex:969: Kernel.Typespec.compile_error/2
1416+
(elixir 1.17.2) lib/kernel/typespec.ex:307: Kernel.Typespec.translate_type/2
1417+
(stdlib 6.0) lists.erl:2343: :lists.mapfoldl_1/3
1418+
(elixir 1.17.2) lib/kernel/typespec.ex:235: Kernel.Typespec.translate_typespecs_for_module/2
1419+
(elixir 1.17.2) src/elixir_erl.erl:121: :elixir_erl.compile/1
1420+
(elixir 1.17.2) src/elixir_module.erl:190: anonymous fn/9 in :elixir_module.compile/7
1421+
13631422
"""
13641423

13651424
defexception [:file, :line, :description]
@@ -1374,6 +1433,17 @@ defmodule Kernel.TypespecError do
13741433
end
13751434

13761435
defmodule BadFunctionError do
1436+
@moduledoc """
1437+
An exception raised when something expected a function, but received something else.
1438+
1439+
For example, this exception gets raised when a function is expected but a string is passed instead:
1440+
1441+
iex> value = "hello"
1442+
value.()
1443+
** (BadFunctionError) expected a function, got: "hello"
1444+
1445+
"""
1446+
13771447
defexception [:term]
13781448

13791449
@impl true
@@ -1400,6 +1470,13 @@ end
14001470
defmodule BadMapError do
14011471
@moduledoc """
14021472
An exception raised when something expected a map, but received something else.
1473+
For example this exception gets raised when a map is expected but a string is passed instead:
1474+
1475+
iex> value = "hello"
1476+
%{value | key: "value"}
1477+
** (BadMapError) expected a map, got: "hello"
1478+
(stdlib 6.0) :maps.put(:k, :v, "hello")
1479+
14031480
"""
14041481

14051482
defexception [:term]
@@ -1413,6 +1490,31 @@ end
14131490
defmodule BadBooleanError do
14141491
@moduledoc """
14151492
An exception raised when an operator expected a boolean, but received something else.
1493+
for example, this exception gets raised operator expected a boolean but receive other data types this error is a runtime error that occur when the value is strictly boolean:
1494+
iex> defmodule Example do
1495+
def test(value) do
1496+
if is_boolean(value) and value do
1497+
IO.puts("Boolean value is true.")
1498+
else
1499+
raise BadBooleanError, "Expected a boolean"
1500+
end
1501+
end
1502+
end
1503+
1504+
Example.test(:ok)
1505+
1506+
** (FunctionClauseError) no function clause matching in BadBooleanError.exception/1
1507+
1508+
The following arguments were given to BadBooleanError.exception/1:
1509+
1510+
# 1
1511+
"Expected a boolean, got: :ok"
1512+
1513+
Attempted function clauses (showing 1 out of 1):
1514+
1515+
def exception(args) when is_list(args)
1516+
1517+
14161518
"""
14171519

14181520
defexception [:term, :operator]
@@ -1531,6 +1633,15 @@ defmodule TryClauseError do
15311633
15321634
* `:term` (`t:term/0`) - the term that did not match any of the clauses
15331635
1636+
for example, this exception does not match any of the term:
1637+
iex> try do
1638+
throw(:unexpected_value)
1639+
rescue
1640+
ArgumentError -> IO.puts("ArgumentError handled")
1641+
end
1642+
1643+
** (throw) :unexpected_value
1644+
15341645
"""
15351646
defexception [:term]
15361647

@@ -1543,6 +1654,15 @@ end
15431654
defmodule BadArityError do
15441655
@moduledoc """
15451656
An exception raised when a function is called with the wrong number of arguments.
1657+
for example, this exception gets raised when a function is called with the wrong number of arguments:
1658+
1659+
iex> my_function = fn x, y -> x + y end
1660+
my_function.(1)
1661+
1662+
** (BadArityError) #Function<41.39164016/2 in :erl_eval.expr/6> with arity 2 called with 1 argument (1)
1663+
(stdlib 6.0) erl_eval.erl:870: :erl_eval.do_apply/6
1664+
1665+
15461666
"""
15471667

15481668
defexception [:function, :args]
@@ -1959,6 +2079,10 @@ defmodule Code.LoadError do
19592079
19602080
* `:file` (`t:String.t/0`) - the file name
19612081
* `:reason` (`t:term/0`) - the reason why the file could not be loaded
2082+
for example, this exception gets raised when a file cannot be loaded:
2083+
2084+
iex> Code.require_file("missing_file.exs")
2085+
** (Code.LoadError) could not load non_existent.ex. Reason: enoent
19622086
19632087
"""
19642088

@@ -2061,6 +2185,13 @@ defmodule KeyError do
20612185
* `:term` (`t:term/0`) - the data structure that was searched
20622186
* `:key` (`t:term/0`) - the key that was not found
20632187
2188+
iex> map = %{name: "Alice", age: 25}
2189+
name = Map.fetch!(map, :gender)
2190+
2191+
** (KeyError) key :gender not found in: %{name: "Alice", age: 25}
2192+
(stdlib 6.0) :maps.get(:gender, %{name: "Alice",Regex.CompileError age: 25})
2193+
2194+
20642195
"""
20652196

20662197
defexception [:key, :term, :message]
@@ -2138,6 +2269,12 @@ defmodule KeyError do
21382269
end
21392270

21402271
defmodule UnicodeConversionError do
2272+
@moduledoc """
2273+
An exception raised when there is an issue converting data to or from Unicode.
2274+
his exception typically occurs when working with strings or binaries that are invalid UTF-8.
2275+
2276+
2277+
"""
21412278
defexception [:encoded, :message]
21422279

21432280
def exception(opts) do
@@ -2216,6 +2353,13 @@ defmodule Enum.OutOfBoundsError do
22162353
a certain size but finds that it is too small.
22172354
22182355
For example, this is raised by `Access.at!/1`.
2356+
2357+
list = []
2358+
Enum.fetch!(list, 0)
2359+
2360+
** (Enum.OutOfBoundsError) out of bounds error
2361+
(elixir 1.17.2) lib/enum.ex:1084: Enum.fetch!/2
2362+
22192363
"""
22202364

22212365
defexception [:enumerable, :index, :message]
@@ -2245,6 +2389,12 @@ defmodule Enum.EmptyError do
22452389
but finds an empty one.
22462390
22472391
For example, this is raised by `Enum.min/3`.
2392+
2393+
iex> list = []
2394+
Enum.min(list)
2395+
** (Enum.EmptyError) empty error
2396+
(elixir 1.17.2) lib/enum.ex:2084: anonymous fn/0 in Enum.min/1
2397+
22482398
"""
22492399

22502400
defexception message: "empty error"
@@ -2259,6 +2409,11 @@ defmodule File.Error do
22592409
* `:path` (`t:Path.t/0`) - the path of the file that caused the error
22602410
* `:reason` (`t:File.posix/0`) - the reason for the error
22612411
2412+
For example, this exception is raised, when trying to read a non existent file:
2413+
iex> File.read("nonexistent_file.txt")
2414+
{:error, :enoent}
2415+
2416+
22622417
"""
22632418

22642419
defexception [:reason, :path, action: ""]
@@ -2288,6 +2443,10 @@ defmodule File.CopyError do
22882443
* `:destination` (`t:Path.t/0`) - the destination path
22892444
* `:reason` (`t:File.posix/0`) - the reason why the file could not be copied
22902445
2446+
For example, this exception is raised when trying to copy to file or directory that isn't present
2447+
iex> File.cp_r("source_dir", "source_dir/subdir")
2448+
{:error, :enoent, "source_dir"}
2449+
22912450
"""
22922451

22932452
defexception [:reason, :source, :destination, on: "", action: ""]
@@ -2317,6 +2476,10 @@ defmodule File.RenameError do
23172476
* `:destination` (`t:Path.t/0`) - the destination path
23182477
* `:reason` (`t:File.posix/0`) - the reason why the file could not be renamed
23192478
2479+
for example this exception is raised when trying to rename a file that isn't present
2480+
iex> File.rename("source.txt", "target.txt")
2481+
{:error, :enoent, "source.txt"}
2482+
23202483
"""
23212484

23222485
defexception [:reason, :source, :destination, on: "", action: ""]
@@ -2346,6 +2509,12 @@ defmodule File.LinkError do
23462509
* `:new` (`t:Path.t/0`) - the link destination
23472510
* `:reason` (`t:File.posix/0`) - the reason why the file could not be linked
23482511
2512+
For example, an exception error that can happen:
2513+
File.write!("source.txt", "content")
2514+
File.write!("target.txt", "existing content")
2515+
File.ln("source.txt", "target.txt")
2516+
errorMessage: {:error, :eexist}
2517+
23492518
"""
23502519

23512520
defexception [:reason, :existing, :new, action: ""]
@@ -2360,6 +2529,14 @@ defmodule File.LinkError do
23602529
end
23612530

23622531
defmodule ErlangError do
2532+
@moduledoc """
2533+
An exception raised when an Erlang error is encountered while compliling an Erlang code
2534+
for example using :erlang with a function that isn't define on erlang.
2535+
:erlang.error(:some_invalid_error)
2536+
2537+
** (ErlangError) Erlang error: :some_invalid_errors
2538+
"""
2539+
23632540
defexception [:original, :reason]
23642541

23652542
@impl true

0 commit comments

Comments
 (0)