-
Notifications
You must be signed in to change notification settings - Fork 0
/
10-exceptions.cfc
57 lines (48 loc) · 1.85 KB
/
10-exceptions.cfc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
component extends="../BaseTask" {
function compute(){
print.greenLine( "Computing from: #getThreadname()#" )
return 2;
}
function create(){
return asyncManager.newFuture( () => compute() )
}
function run(){
print.blueLine( "Starting from: #getThreadname()#" )
var future = create()
.then( ( data ) => data * 2 )
.then( ( data ) => data / 0 )
.onException( ( e ) => {
// e => Java Runtime Exception
// All exceptions in Futures are wrapped in a `CompletionException`.
// You can get the underlying exception by calling `getCause()`.
if ( "java.util.concurrent.CompletionException" == e.getClass().getName() ) {
e = e.getCause();
}
print.redLine( "Oops! Something blew up => " & e.getMessage() ).toConsole();
print.redLine( " Returning 1 instead." ).toConsole();
return 1;
} )
.then( ( data ) => data / 0 )
.handle( ( data, e ) => {
if ( !isNull( e ) ) {
// All exceptions in Futures are wrapped in a `CompletionException`.
// You can get the underlying exception by calling `getCause()`.
if ( "java.util.concurrent.CompletionException" == e.getClass().getName() ) {
e = e.getCause();
}
print.redLine( "Oops! Something blew up => " & e.getMessage() ).toConsole();
print.redLine( " Returning 0 instead." ).toConsole();
return 0;
}
return data + 1;
} )
.then( ( data ) => data + 1 )
.thenRun( ( data ) => print.boldGreenLine( "Final Result: " & data ).toConsole() )
print.blueLine( "Future Result: #future.get()#" )
// 1. Let's add a divide by zero error, let it blew up
// 2. Add the onException() to handle the previous stages and recover
// 3. What happens if not return value is returned? blows up again, data is undefined.
// 4. You can add as many onExceptions() as you see fit
// 5. You can also use the handle() function
}
}