vault backup: 2023-09-05 15:54:27
This commit is contained in:
parent
da42ed5332
commit
c599aadc51
@ -17,4 +17,35 @@ if cond then expr1 else expr2
|
|||||||
# let abs(x) =
|
# let abs(x) =
|
||||||
if x>0 then x
|
if x>0 then x
|
||||||
else -x ;;;
|
else -x ;;;
|
||||||
|
val abs : int -> int <fun>
|
||||||
|
```
|
||||||
|
|
||||||
|
## 3.2. Exceptions
|
||||||
|
### Division by 0:
|
||||||
|
```Ocaml
|
||||||
|
# 1/0
|
||||||
|
Exception : Division by zero
|
||||||
|
```
|
||||||
|
|
||||||
|
### Failwith:
|
||||||
|
Failwith is a function that take a string argument (the reason) and return the error. That way, we can raise an error into our code
|
||||||
|
```Ocaml
|
||||||
|
# failwith;;
|
||||||
|
-: string -> a = <fun>
|
||||||
|
|
||||||
|
# failwith "oops";;
|
||||||
|
Exception : Failwith "oops"
|
||||||
|
```
|
||||||
|
|
||||||
|
### invalid_arg:
|
||||||
|
Invalid_arg is a function that take a string argument (the reason) and return the error. That way, we can raison an error about an argument
|
||||||
|
```Ocaml
|
||||||
|
# invalid_arg "oops";
|
||||||
|
Exception: Invalid_argument "oops"
|
||||||
|
|
||||||
|
(*example*)
|
||||||
|
# let div a b =
|
||||||
|
if b = 0 then invalid_arg("div: b = 0")
|
||||||
|
else a/b ;;;
|
||||||
|
val div int -> int -> int = <fun>
|
||||||
```
|
```
|
||||||
|
Loading…
x
Reference in New Issue
Block a user