-
Notifications
You must be signed in to change notification settings - Fork 3
/
Exception.cpp
52 lines (49 loc) · 1.34 KB
/
Exception.cpp
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
#include <detail/Exception.hpp>
#include <iostream>
#include <string>
#include <cstdlib>
namespace mylib
{
namespace detail
{
namespace exception
{
// Rethrow (creates a std::nested_exception) an exception, using the Exception class
// which contains file and line info. The original exception is preserved...
void rethrow(const std::string & message, const char * file, unsigned int line)
try
{
std::rethrow_exception(std::current_exception());
}
catch (...)
{
std::throw_with_nested(Exception(message, file, line));
}
// Backtrace an exception by recursively unwrapping the nested exceptions
void Backtrace(const std::exception & ex)
try
{
std::cerr << ex.what() << std::endl;
rethrow_if_nested(ex);
}
catch( const std::exception & nested_ex )
{
Backtrace(nested_ex);
}
// General Exception handler
void Handle_Exception(const std::exception & ex, const std::string & function)
try
{
if (function != "")
std::cerr << "Library API: Exception caught in function \'" << function << "\'" << std::endl;
std::cerr << "Backtrace:" << std::endl;
Backtrace(ex);
}
catch( ... )
{
std::cerr << "Something went super-wrong! TERMINATING!" << std::endl;
std::exit( EXIT_FAILURE );
}
}
}
}