Skip to content

Commit 1eb3a20

Browse files
Improve docs and add examples to exception.ex (#14227)
1 parent f94f4d5 commit 1eb3a20

File tree

1 file changed

+133
-9
lines changed

1 file changed

+133
-9
lines changed

lib/elixir/lib/exception.ex

Lines changed: 133 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,7 +1040,15 @@ defmodule ArgumentError do
10401040
An exception raised when an argument to a function is invalid.
10411041
10421042
You can raise this exception when you want to signal that an argument to
1043-
a function is invalid.
1043+
a function is invalid. For example, this exception is raised when calling
1044+
`Integer.to_string/1` with an invalid argument:
1045+
1046+
iex> Integer.to_string(1.0)
1047+
** (ArgumentError) errors were found at the given arguments:
1048+
1049+
* 1st argument: not an integer
1050+
1051+
:erlang.integer_to_binary(1.0)
10441052
10451053
`ArgumentError` exceptions have a single field, `:message` (a `t:String.t/0`),
10461054
which is public and can be accessed freely when reading or creating `ArgumentError`
@@ -1104,7 +1112,11 @@ defmodule SystemLimitError do
11041112
@moduledoc """
11051113
An exception raised when a system limit has been reached.
11061114
1107-
For example, this can happen if you try to create an atom that is too large.
1115+
For example, this can happen if you try to create an atom that is too large:
1116+
1117+
iex> String.to_atom(String.duplicate("a", 100_000))
1118+
** (SystemLimitError) a system limit has been reached
1119+
11081120
"""
11091121

11101122
defexception message: "a system limit has been reached"
@@ -1252,6 +1264,8 @@ defmodule TokenMissingError do
12521264
* `:opening_delimiter` - an atom representing the opening delimiter
12531265
* `:expected_delimiter` - an atom representing the expected delimiter
12541266
* `:description` - a description of the missing token error
1267+
1268+
This is mostly raised by Elixir tooling when compiling and evaluating code.
12551269
"""
12561270

12571271
defexception [
@@ -1337,6 +1351,7 @@ defmodule CompileError do
13371351
the error occurred in code that did not come from a file
13381352
* `:line` (`t:non_neg_integer/0`) - the line where the error occurred
13391353
1354+
This is mostly raised by Elixir tooling when compiling and evaluating code.
13401355
"""
13411356

13421357
defexception [:file, :line, description: "compile error"]
@@ -1360,6 +1375,14 @@ defmodule Kernel.TypespecError do
13601375
the error occurred in code that did not come from a file
13611376
* `:line` (`t:non_neg_integer/0`) - the line where the error occurred
13621377
1378+
For example, if your typespec definition points to an invalid type, you get an exception:
1379+
1380+
@type my_type :: intger()
1381+
1382+
will raise:
1383+
1384+
** (Kernel.TypespecError) type strng/0 undefined
1385+
13631386
"""
13641387

13651388
defexception [:file, :line, :description]
@@ -1374,6 +1397,17 @@ defmodule Kernel.TypespecError do
13741397
end
13751398

13761399
defmodule BadFunctionError do
1400+
@moduledoc """
1401+
An exception raised when a function is expected, but something else was given.
1402+
1403+
For example:
1404+
1405+
iex> value = "hello"
1406+
value.()
1407+
** (BadFunctionError) expected a function, got: "hello"
1408+
1409+
"""
1410+
13771411
defexception [:term]
13781412

13791413
@impl true
@@ -1399,7 +1433,14 @@ end
13991433

14001434
defmodule BadMapError do
14011435
@moduledoc """
1402-
An exception raised when something expected a map, but received something else.
1436+
An exception raised when a map is expected, but something else was given.
1437+
1438+
For example:
1439+
1440+
iex> value = "hello"
1441+
%{value | key: "value"}
1442+
** (BadMapError) expected a map, got: "hello"
1443+
14031444
"""
14041445

14051446
defexception [:term]
@@ -1412,7 +1453,13 @@ end
14121453

14131454
defmodule BadBooleanError do
14141455
@moduledoc """
1415-
An exception raised when an operator expected a boolean, but received something else.
1456+
An exception raised when a boolean is expected, but something else was given.
1457+
1458+
This exception is raised by `and` and `or` when the first argument is not a boolean:
1459+
1460+
iex(1)> 123 and true
1461+
** (BadBooleanError) expected a boolean on left-side of "and", got: 123
1462+
14161463
"""
14171464

14181465
defexception [:term, :operator]
@@ -1524,8 +1571,19 @@ end
15241571

15251572
defmodule TryClauseError do
15261573
@moduledoc """
1527-
An exception raised when a term in a `try/1` expression
1528-
does not match any of the defined `->` clauses in its `else`.
1574+
An exception raised when none of the `else` clauses in a `try/1` match.
1575+
1576+
For example:
1577+
1578+
iex> try do
1579+
...> :ok
1580+
...> rescue
1581+
...> e -> e
1582+
...> else
1583+
...> # :ok -> :ok is missing
1584+
...> :not_ok -> :not_ok
1585+
...> end
1586+
** (TryClauseError) no try clause matching: :ok
15291587
15301588
The following fields of this exception are public and can be accessed freely:
15311589
@@ -1543,6 +1601,13 @@ end
15431601
defmodule BadArityError do
15441602
@moduledoc """
15451603
An exception raised when a function is called with the wrong number of arguments.
1604+
1605+
For example:
1606+
1607+
iex> my_function = fn x, y -> x + y end
1608+
iex> my_function.(42)
1609+
** (BadArityError) #Function<41.39164016/2 in :erl_eval.expr/6> with arity 2 called with 1 argument (42)
1610+
15461611
"""
15471612

15481613
defexception [:function, :args]
@@ -1955,6 +2020,11 @@ defmodule Code.LoadError do
19552020
@moduledoc """
19562021
An exception raised when a file cannot be loaded.
19572022
2023+
This is typically raised by functions in the `Code` module, for example:
2024+
2025+
iex> Code.require_file("missing_file.exs")
2026+
** (Code.LoadError) could not load missing_file.exs. Reason: enoent
2027+
19582028
The following fields of this exception are public and can be accessed freely:
19592029
19602030
* `:file` (`t:String.t/0`) - the file name
@@ -2054,7 +2124,11 @@ defmodule KeyError do
20542124
An exception raised when a key is not found in a data structure.
20552125
20562126
For example, this is raised by `Map.fetch!/2` when the given key
2057-
cannot be found in the given map.
2127+
cannot be found in the given map:
2128+
2129+
iex> map = %{name: "Alice", age: 25}
2130+
iex> Map.fetch!(map, :first_name)
2131+
** (KeyError) key :first_name not found in: %{name: "Alice", age: 25}
20582132
20592133
The following fields of this exception are public and can be accessed freely:
20602134
@@ -2138,6 +2212,15 @@ defmodule KeyError do
21382212
end
21392213

21402214
defmodule UnicodeConversionError do
2215+
@moduledoc """
2216+
An exception raised when converting data to or from Unicode.
2217+
2218+
For example:
2219+
2220+
iex> String.to_charlist(<<0xFF>>)
2221+
** (UnicodeConversionError) invalid encoding starting at <<255>>
2222+
2223+
"""
21412224
defexception [:encoded, :message]
21422225

21432226
def exception(opts) do
@@ -2215,7 +2298,11 @@ defmodule Enum.OutOfBoundsError do
22152298
An exception that is raised when a function expects an enumerable to have
22162299
a certain size but finds that it is too small.
22172300
2218-
For example, this is raised by `Access.at!/1`.
2301+
For example:
2302+
2303+
iex> Enum.fetch!([1, 2, 3], 5)
2304+
** (Enum.OutOfBoundsError) out of bounds error
2305+
22192306
"""
22202307

22212308
defexception [:enumerable, :index, :message]
@@ -2244,7 +2331,11 @@ defmodule Enum.EmptyError do
22442331
An exception that is raised when something expects a non-empty enumerable
22452332
but finds an empty one.
22462333
2247-
For example, this is raised by `Enum.min/3`.
2334+
For example:
2335+
2336+
iex> Enum.min([])
2337+
** (Enum.EmptyError) empty error
2338+
22482339
"""
22492340

22502341
defexception message: "empty error"
@@ -2254,6 +2345,11 @@ defmodule File.Error do
22542345
@moduledoc """
22552346
An exception that is raised when a file operation fails.
22562347
2348+
For example, this exception is raised, when trying to read a non existent file:
2349+
2350+
iex> File.read!("nonexistent_file.txt")
2351+
** (File.Error) could not read file "nonexistent_file.txt": no such file or directory
2352+
22572353
The following fields of this exception are public and can be accessed freely:
22582354
22592355
* `:path` (`t:Path.t/0`) - the path of the file that caused the error
@@ -2282,6 +2378,11 @@ defmodule File.CopyError do
22822378
@moduledoc """
22832379
An exception that is raised when copying a file fails.
22842380
2381+
For example, this exception is raised when trying to copy to file or directory that isn't present:
2382+
2383+
iex> File.cp_r!("non_existent", "source_dir/subdir")
2384+
** (File.CopyError) could not copy recursively from "non_existent" to "source_dir/subdir". non_existent: no such file or directory
2385+
22852386
The following fields of this exception are public and can be accessed freely:
22862387
22872388
* `:source` (`t:Path.t/0`) - the source path
@@ -2311,6 +2412,11 @@ defmodule File.RenameError do
23112412
@moduledoc """
23122413
An exception that is raised when renaming a file fails.
23132414
2415+
For example, this exception is raised when trying to rename a file that isn't present:
2416+
2417+
iex> File.rename!("source.txt", "target.txt")
2418+
** (File.RenameError) could not rename from "source.txt" to "target.txt": no such file or directory
2419+
23142420
The following fields of this exception are public and can be accessed freely:
23152421
23162422
* `:source` (`t:Path.t/0`) - the source path
@@ -2340,6 +2446,11 @@ defmodule File.LinkError do
23402446
@moduledoc """
23412447
An exception that is raised when linking a file fails.
23422448
2449+
For example, this exception is raised when trying to link to file that isn't present:
2450+
2451+
iex> File.ln!("source.txt", "target.txt")
2452+
** (File.LinkError) could not create hard link from "source.txt" to "target.txt": no such file or directory
2453+
23432454
The following fields of this exception are public and can be accessed freely:
23442455
23452456
* `:existing` (`t:Path.t/0`) - the existing file to link
@@ -2360,6 +2471,19 @@ defmodule File.LinkError do
23602471
end
23612472

23622473
defmodule ErlangError do
2474+
@moduledoc """
2475+
An exception raised when invoking an Erlang code that errors
2476+
with a value not handled by Elixir.
2477+
2478+
Most common error reasons, such as `:badarg` are automatically
2479+
converted into exceptions by Elixir. However, you may invoke some
2480+
code that emits a custom error reason and those get wrapped into
2481+
`ErlangError`:
2482+
2483+
iex> :erlang.error(:some_invalid_error)
2484+
** (ErlangError) Erlang error: :some_invalid_error
2485+
"""
2486+
23632487
defexception [:original, :reason]
23642488

23652489
@impl true

0 commit comments

Comments
 (0)