@@ -1040,7 +1040,15 @@ defmodule ArgumentError do
1040
1040
An exception raised when an argument to a function is invalid.
1041
1041
1042
1042
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)
1044
1052
1045
1053
`ArgumentError` exceptions have a single field, `:message` (a `t:String.t/0`),
1046
1054
which is public and can be accessed freely when reading or creating `ArgumentError`
@@ -1104,7 +1112,11 @@ defmodule SystemLimitError do
1104
1112
@ moduledoc """
1105
1113
An exception raised when a system limit has been reached.
1106
1114
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
+
1108
1120
"""
1109
1121
1110
1122
defexception message: "a system limit has been reached"
@@ -1252,6 +1264,8 @@ defmodule TokenMissingError do
1252
1264
* `:opening_delimiter` - an atom representing the opening delimiter
1253
1265
* `:expected_delimiter` - an atom representing the expected delimiter
1254
1266
* `:description` - a description of the missing token error
1267
+
1268
+ This is mostly raised by Elixir tooling when compiling and evaluating code.
1255
1269
"""
1256
1270
1257
1271
defexception [
@@ -1337,6 +1351,7 @@ defmodule CompileError do
1337
1351
the error occurred in code that did not come from a file
1338
1352
* `:line` (`t:non_neg_integer/0`) - the line where the error occurred
1339
1353
1354
+ This is mostly raised by Elixir tooling when compiling and evaluating code.
1340
1355
"""
1341
1356
1342
1357
defexception [ :file , :line , description: "compile error" ]
@@ -1360,6 +1375,14 @@ defmodule Kernel.TypespecError do
1360
1375
the error occurred in code that did not come from a file
1361
1376
* `:line` (`t:non_neg_integer/0`) - the line where the error occurred
1362
1377
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
+
1363
1386
"""
1364
1387
1365
1388
defexception [ :file , :line , :description ]
@@ -1374,6 +1397,17 @@ defmodule Kernel.TypespecError do
1374
1397
end
1375
1398
1376
1399
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
+
1377
1411
defexception [ :term ]
1378
1412
1379
1413
@ impl true
@@ -1399,7 +1433,14 @@ end
1399
1433
1400
1434
defmodule BadMapError do
1401
1435
@ 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
+
1403
1444
"""
1404
1445
1405
1446
defexception [ :term ]
@@ -1412,7 +1453,13 @@ end
1412
1453
1413
1454
defmodule BadBooleanError do
1414
1455
@ 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
+
1416
1463
"""
1417
1464
1418
1465
defexception [ :term , :operator ]
@@ -1524,8 +1571,19 @@ end
1524
1571
1525
1572
defmodule TryClauseError do
1526
1573
@ 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
1529
1587
1530
1588
The following fields of this exception are public and can be accessed freely:
1531
1589
@@ -1543,6 +1601,13 @@ end
1543
1601
defmodule BadArityError do
1544
1602
@ moduledoc """
1545
1603
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
+
1546
1611
"""
1547
1612
1548
1613
defexception [ :function , :args ]
@@ -1955,6 +2020,11 @@ defmodule Code.LoadError do
1955
2020
@ moduledoc """
1956
2021
An exception raised when a file cannot be loaded.
1957
2022
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
+
1958
2028
The following fields of this exception are public and can be accessed freely:
1959
2029
1960
2030
* `:file` (`t:String.t/0`) - the file name
@@ -2054,7 +2124,11 @@ defmodule KeyError do
2054
2124
An exception raised when a key is not found in a data structure.
2055
2125
2056
2126
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}
2058
2132
2059
2133
The following fields of this exception are public and can be accessed freely:
2060
2134
@@ -2138,6 +2212,15 @@ defmodule KeyError do
2138
2212
end
2139
2213
2140
2214
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
+ """
2141
2224
defexception [ :encoded , :message ]
2142
2225
2143
2226
def exception ( opts ) do
@@ -2215,7 +2298,11 @@ defmodule Enum.OutOfBoundsError do
2215
2298
An exception that is raised when a function expects an enumerable to have
2216
2299
a certain size but finds that it is too small.
2217
2300
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
+
2219
2306
"""
2220
2307
2221
2308
defexception [ :enumerable , :index , :message ]
@@ -2244,7 +2331,11 @@ defmodule Enum.EmptyError do
2244
2331
An exception that is raised when something expects a non-empty enumerable
2245
2332
but finds an empty one.
2246
2333
2247
- For example, this is raised by `Enum.min/3`.
2334
+ For example:
2335
+
2336
+ iex> Enum.min([])
2337
+ ** (Enum.EmptyError) empty error
2338
+
2248
2339
"""
2249
2340
2250
2341
defexception message: "empty error"
@@ -2254,6 +2345,11 @@ defmodule File.Error do
2254
2345
@ moduledoc """
2255
2346
An exception that is raised when a file operation fails.
2256
2347
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
+
2257
2353
The following fields of this exception are public and can be accessed freely:
2258
2354
2259
2355
* `:path` (`t:Path.t/0`) - the path of the file that caused the error
@@ -2282,6 +2378,11 @@ defmodule File.CopyError do
2282
2378
@ moduledoc """
2283
2379
An exception that is raised when copying a file fails.
2284
2380
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
+
2285
2386
The following fields of this exception are public and can be accessed freely:
2286
2387
2287
2388
* `:source` (`t:Path.t/0`) - the source path
@@ -2311,6 +2412,11 @@ defmodule File.RenameError do
2311
2412
@ moduledoc """
2312
2413
An exception that is raised when renaming a file fails.
2313
2414
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
+
2314
2420
The following fields of this exception are public and can be accessed freely:
2315
2421
2316
2422
* `:source` (`t:Path.t/0`) - the source path
@@ -2340,6 +2446,11 @@ defmodule File.LinkError do
2340
2446
@ moduledoc """
2341
2447
An exception that is raised when linking a file fails.
2342
2448
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
+
2343
2454
The following fields of this exception are public and can be accessed freely:
2344
2455
2345
2456
* `:existing` (`t:Path.t/0`) - the existing file to link
@@ -2360,6 +2471,19 @@ defmodule File.LinkError do
2360
2471
end
2361
2472
2362
2473
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
+
2363
2487
defexception [ :original , :reason ]
2364
2488
2365
2489
@ impl true
0 commit comments