14
14
15
15
#pragma once
16
16
17
+ #include " glog/logging.h"
17
18
#include " paddle/fluid/platform/enforce.h"
18
19
19
20
namespace paddle {
@@ -22,55 +23,66 @@ namespace details {
22
23
23
24
class ExceptionHolder {
24
25
public:
25
- void Catch (const platform::EnforceNotMet& exp) {
26
- std::lock_guard<std::mutex> lock (mu_);
27
- exception_.reset (new platform::EnforceNotMet (exp));
28
- type_ = kEnforceNotMet ;
29
- }
30
-
31
- void Catch (const platform::EOFException& exp) {
32
- std::lock_guard<std::mutex> lock (mu_);
33
- // EOFException will not cover up existing EnforceNotMet.
34
- if (exception_.get () == nullptr ) {
35
- exception_.reset (new platform::EOFException (exp));
36
- type_ = kEOF ;
26
+ void Catch (std::exception_ptr eptr) {
27
+ try {
28
+ std::rethrow_exception (eptr);
29
+ } catch (platform::EOFException exp) {
30
+ Catch (exp);
31
+ } catch (platform::EnforceNotMet exp) {
32
+ Catch (exp);
33
+ } catch (...) {
34
+ LOG (FATAL) << " Unknown exception caught" ;
37
35
}
38
36
}
39
37
40
- bool ExceptionCatched () const {
38
+ bool IsCaught () const {
41
39
std::lock_guard<std::mutex> lock (mu_);
42
40
return exception_.get () != nullptr ;
43
41
}
44
42
45
- void Throw () {
43
+ void ReThrow () {
46
44
std::lock_guard<std::mutex> lock (mu_);
47
45
switch (type_) {
48
46
case kNone :
49
47
break ;
50
48
case kEnforceNotMet : {
51
49
auto e = *static_cast <platform::EnforceNotMet*>(exception_.get ());
52
50
throw e;
53
- break ;
54
51
}
55
52
case kEOF : {
56
53
auto e = *static_cast <platform::EOFException*>(exception_.get ());
57
54
throw e;
58
- break ;
59
55
}
60
- default :
61
- LOG (FATAL) << " Unknown exception." ;
62
56
}
63
- exception_.reset ();
64
- type_ = kNone ;
57
+ ClearImpl ();
65
58
}
66
59
67
60
void Clear () {
68
61
std::lock_guard<std::mutex> lock (mu_);
62
+ ClearImpl ();
63
+ }
64
+
65
+ private:
66
+ void ClearImpl () {
69
67
exception_.reset ();
70
68
type_ = kNone ;
71
69
}
72
70
73
- private:
71
+ void Catch (const platform::EnforceNotMet& exp) {
72
+ std::lock_guard<std::mutex> lock (mu_);
73
+ exception_.reset (new platform::EnforceNotMet (exp));
74
+ type_ = kEnforceNotMet ;
75
+ }
76
+
77
+ void Catch (const platform::EOFException& exp) {
78
+ std::lock_guard<std::mutex> lock (mu_);
79
+ // EOFException will not cover up existing EnforceNotMet.
80
+ if (exception_.get () == nullptr ) {
81
+ exception_.reset (new platform::EOFException (exp));
82
+ type_ = kEOF ;
83
+ }
84
+ }
85
+
74
86
enum ExceptionType { kNone , kEnforceNotMet , kEOF };
75
87
ExceptionType type_{kNone };
76
88
0 commit comments