Skip to content

Commit 44eaa33

Browse files
committed
Fix signature of throw and add throwAny
1 parent 20d644e commit 44eaa33

File tree

4 files changed

+26
-31
lines changed

4 files changed

+26
-31
lines changed

runtime/Pervasives.res

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,32 @@ Raises the given exception, terminating execution unless caught by a surrounding
2323
## Examples
2424
2525
```rescript
26-
let error = Error.make("Everything is upside down.")
26+
exception MyException(string)
2727
28-
if 5 > 10 {
29-
throw(error)
30-
} else {
31-
Console.log("Phew, sanity still rules.")
28+
try {
29+
throw(MyException("Error message"))
30+
} catch {
31+
| MyException(message) => Console.log("Caught exception: " + message)
3232
}
3333
```
3434
*/
35-
external throw: Stdlib_Error.t => 'a = "%raise"
35+
external throw: exn => 'a = "%raise"
36+
37+
/**
38+
Raises the given exception, terminating execution unless caught by a surrounding try/catch block.
39+
40+
## Examples
41+
42+
```rescript
43+
try {
44+
// You can throw any JS value, but it is not good practice
45+
throwAny(42)
46+
} catch {
47+
| Exn.Error(error) => Console.log("Caught exception: " + error->String.make)
48+
}
49+
```
50+
*/
51+
external throwAny: 'a => 'b = "%raise"
3652

3753
/* Composition operators */
3854

runtime/Stdlib_Error.res

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ module URIError = {
4040

4141
external raise: t => 'a = "%raise"
4242

43-
external throw: t => 'a = "%raise"
44-
45-
let panic = msg => make(`Panic! ${msg}`)->raise
43+
let panic = msg => make(`Panic! ${msg}`)->toException->throw
4644

4745
external ignore: t => unit = "%ignore"

runtime/Stdlib_Error.resi

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -149,34 +149,15 @@ Raises (throws in JavaScript language) the provided `Error.t`, which will stop e
149149
let error = Error.make("Everything is upside down.")
150150
151151
if 5 > 10 {
152-
error->Error.raise
152+
throw(error->Error.toException)
153153
} else {
154154
Console.log("Phew, sanity still rules.")
155155
}
156156
```
157157
*/
158-
@deprecated(
159-
"`raise` has been renamed to `throw` to align with JavaScript vocabulary. Please use `throw` instead"
160-
)
158+
@deprecated("Please use the global `throw` or `throwAny` function instead")
161159
external raise: t => 'a = "%raise"
162160

163-
/**
164-
Raises the given exception, terminating execution unless caught by a surrounding try/catch block.
165-
166-
## Examples
167-
168-
```rescript
169-
let error = Error.make("Everything is upside down.")
170-
171-
if 5 > 10 {
172-
Error.throw(error)
173-
} else {
174-
Console.log("Phew, sanity still rules.")
175-
}
176-
```
177-
*/
178-
external throw: t => 'a = "%raise"
179-
180161
/**
181162
Raises a panic exception with the given message.
182163

runtime/Stdlib_Int.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ let range = (start, end, ~options: rangeOptions={}) => {
6464
let step = switch options.step {
6565
| None => isInverted ? -1 : 1
6666
| Some(0) if start !== end =>
67-
Stdlib_Error.throw(Stdlib_Error.RangeError.make("Incorrect range arguments"))
67+
throw(Stdlib_Error.RangeError.make("Incorrect range arguments")->Stdlib_Error.toException)
6868
| Some(n) => n
6969
}
7070

0 commit comments

Comments
 (0)