-
Notifications
You must be signed in to change notification settings - Fork 247
Expand file tree
/
Copy patherror_handle.cpp
More file actions
61 lines (54 loc) · 1.95 KB
/
error_handle.cpp
File metadata and controls
61 lines (54 loc) · 1.95 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
#include <cstdio>
#include "uTensor/core/context.hpp"
#include "errorHandler.hpp"
using namespace std;
using namespace uTensor;
// declare and define your own event type
// we will try to catch MyEvent but not EventDontCatch
DECLARE_EVENT(MyEvent);
DEFINE_EVENT(MyEvent);
DECLARE_EVENT(EventDontCatch);
DEFINE_EVENT(EventDontCatch);
// declare and define your own error type
DECLARE_ERROR(MyError);
DEFINE_ERROR(MyError);
class MyEventHandler : public ErrorHandler {
public:
// uThrow: normally invoked when a error occurs during op evaluation
// you can override this method and do what ever you want with the error.
// In this tutorial, we're going to implement a spin-wait handler so we
// can debug the error easily in a debugger such as lldb or gdb.
virtual void uThrow(Error* err) override {
if (err->event_id == MyError::uid) {
printf("MyError thrown. Spinning...\n");
while (true) {
}
}
}
// notify: normally invoked when a framework event occurs. For example, in
// the arenaAllocator, events such as the allocator is created will notified
// to the handler. You can do check/logging with it.
// Each event is identified by a static uid.
virtual void notify(const Event& evt) override {
if (evt.event_id == MyEvent::uid) {
printf("MyEvent detected\n");
} else {
printf("Unknown Event detected\n");
}
}
};
int main(int argc, const char** argv) {
MyEventHandler handler;
Context* ptrDefaultContext = Context::get_default_context();
// setup handler
ptrDefaultContext->set_ErrorHandler(&handler);
// this event won't be handled
ptrDefaultContext->notifyEvent(EventDontCatch());
// but this event will be handled
ptrDefaultContext->notifyEvent(MyEvent());
// this error will not be handled in our handler
ptrDefaultContext->throwError(new InvalidTensorError);
// but this error will be handled and cause the process to spin
ptrDefaultContext->throwError(new MyError);
return 0;
}