forked from dlang/dmd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdruntime.filterthrown.dd
More file actions
61 lines (49 loc) · 1.51 KB
/
druntime.filterthrown.dd
File metadata and controls
61 lines (49 loc) · 1.51 KB
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
58
59
60
61
Gravedigger approach to Throwables escaping a thread entry point is now available
A new method is added to `ThreadBase` enabling filtering of any `Throwable`'s before it is handled by the thread abstraction.
This may be used per-thread and globally, to log that an `Error` has occured or to exit the process.
A reasonable error handler that may be of use is:
```d
import core.exception;
void main()
{
filterThreadThrowableHandler = (ref Throwable t) {
import core.stdc.stdio;
import core.stdc.stdlib;
if (auto e = cast(Error) t)
{
auto msg = e.message();
fprintf(stderr, "Thread death due to error: %.*s\n", cast(int)msg.length, msg.ptr);
fflush(stderr);
abort();
}
};
}
```
For a per thread handler the following example may be what you want:
```d
import core.thread;
class MyThread : Thread
{
this( void function() fn, size_t sz = 0 ) @safe pure nothrow @nogc
{
super(fn, sz);
}
this( void delegate() dg, size_t sz = 0 ) @safe pure nothrow @nogc
{
super(dg, sz);
}
override void filterCaughtThrowable(ref Throwable t) @system nothrow
{
import core.stdc.stdio;
import core.stdc.stdlib;
if (auto e = cast(Error) t)
{
auto msg = e.message();
fprintf(stderr, "Thread death due to error: %.*s\n", cast(int)msg.length, msg.ptr);
fflush(stderr);
abort();
}
super.filterCaughtThrowable(t);
}
}
```