Keyple Util C++ Library 2.3.0.5-SNAPSHOT
Reference Terminal Reader API for C++
Exception.h
Go to the documentation of this file.
1/**************************************************************************************************
2 * Copyright (c) 2021 Calypso Networks Association https://calypsonet.org/ *
3 * *
4 * See the NOTICE file(s) distributed with this work for additional information regarding *
5 * copyright ownership. *
6 * *
7 * This program and the accompanying materials are made available under the terms of the Eclipse *
8 * Public License 2.0 which is available at http://www.eclipse.org/legal/epl-2.0 *
9 * *
10 * SPDX-License-Identifier: EPL-2.0 *
11 **************************************************************************************************/
12
13#pragma once
14
15#include <exception>
16#include <memory>
17#include <stdexcept>
18#include <iostream>
19
20namespace keyple {
21namespace core {
22namespace util {
23namespace cpp {
24namespace exception {
25
26class Exception : public std::exception {
27public:
32
36 Exception(const Exception& o) : mMessage(o.mMessage), mCause(o.mCause) {}
37
41 Exception(const std::string& message) : mMessage(message), mCause(nullptr) {}
42
46 Exception(const std::string& message, const std::shared_ptr<Exception> cause)
47 : mMessage(message), mCause(cause) {}
48
52 const std::string& getMessage() const
53 {
54 return mMessage;
55 }
56
60 const std::shared_ptr<Exception> getCause() const
61 {
62 return mCause;
63 }
64
68 const char * what() const noexcept override
69 {
70 return mMessage.c_str();
71 }
72
76 friend std::ostream& operator<<(std::ostream& os, const Exception& e)
77 {
78 os << "EXCEPTION: {"
79 << "MESSAGE = " << e.mMessage;
80
81 if (e.mCause != nullptr) {
82 os << ", CAUSE = " << e.mCause->what();
83 }
84
85 os << "}";
86
87 return os;
88 }
89
93 bool operator==(const Exception& o) const
94 {
95 return mMessage == o.mMessage &&
96 mCause== o.mCause;
97 }
98
99private:
103 const std::string mMessage;
104
108 const std::shared_ptr<Exception> mCause;
109};
110
111}
112}
113}
114}
115}
const std::shared_ptr< Exception > getCause() const
Definition: Exception.h:60
friend std::ostream & operator<<(std::ostream &os, const Exception &e)
Definition: Exception.h:76
Exception(const std::string &message)
Definition: Exception.h:41
bool operator==(const Exception &o) const
Definition: Exception.h:93
Exception(const std::string &message, const std::shared_ptr< Exception > cause)
Definition: Exception.h:46
const std::string & getMessage() const
Definition: Exception.h:52
const char * what() const noexcept override
Definition: Exception.h:68