generated from JMARRUJO91/jazmyne
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5c25cd7
commit 0de260c
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#ifndef __CDMATHEXCEPTION_HXX__ | ||
#define __CDMATHEXCEPTION_HXX__ | ||
|
||
#include <string> | ||
#include <exception> | ||
#include <sstream> // For building formatted error messages | ||
|
||
class CdmathException : public std::exception | ||
{ | ||
public: | ||
CdmathException(const std::string& reason) : _reason(reason) {} | ||
CdmathException(const std::string& reason, const std::string& file, int line) | ||
: _reason(BuildMessage(reason, file, line)) {} | ||
|
||
~CdmathException() throw () {} // Virtual destructor | ||
|
||
const char* what() const throw() override { | ||
return _reason.c_str(); | ||
} | ||
|
||
private: | ||
std::string _reason; | ||
|
||
// Helper function to format the error message | ||
static std::string BuildMessage(const std::string& reason, const std::string& file, int line) { | ||
std::ostringstream oss; | ||
oss << reason << " (File: " << file << ", Line: " << line << ")"; | ||
return oss.str(); | ||
} | ||
}; | ||
|
||
#endif | ||
#include "CdmathException.hxx" | ||
|
||
double divide(double a, double b) { | ||
if (b == 0.0) { | ||
throw CdmathException("Division by zero", __FILE__, __LINE__); | ||
} | ||
return a / b; | ||
} |