You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: lib/elixir/lib/exception.ex
+177Lines changed: 177 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -1045,6 +1045,16 @@ defmodule ArgumentError do
1045
1045
`ArgumentError` exceptions have a single field, `:message` (a `t:String.t/0`),
1046
1046
which is public and can be accessed freely when reading or creating `ArgumentError`
1047
1047
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
(elixir 1.17.2) src/elixir_module.erl:190: anonymous fn/9 in :elixir_module.compile/7
1421
+
1363
1422
"""
1364
1423
1365
1424
defexception[:file,:line,:description]
@@ -1374,6 +1433,17 @@ defmodule Kernel.TypespecError do
1374
1433
end
1375
1434
1376
1435
defmoduleBadFunctionErrordo
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
+
1377
1447
defexception[:term]
1378
1448
1379
1449
@impltrue
@@ -1400,6 +1470,13 @@ end
1400
1470
defmoduleBadMapErrordo
1401
1471
@moduledoc"""
1402
1472
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
+
1403
1480
"""
1404
1481
1405
1482
defexception[:term]
@@ -1413,6 +1490,31 @@ end
1413
1490
defmoduleBadBooleanErrordo
1414
1491
@moduledoc"""
1415
1492
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
+
1416
1518
"""
1417
1519
1418
1520
defexception[:term,:operator]
@@ -1531,6 +1633,15 @@ defmodule TryClauseError do
1531
1633
1532
1634
* `:term` (`t:term/0`) - the term that did not match any of the clauses
1533
1635
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
+
1534
1645
"""
1535
1646
defexception[:term]
1536
1647
@@ -1543,6 +1654,15 @@ end
1543
1654
defmoduleBadArityErrordo
1544
1655
@moduledoc"""
1545
1656
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)
0 commit comments