Kylin/DataStructure/Exception.cpp

68 lines
1.8 KiB
C++
Raw Normal View History

2023-12-27 10:29:16 +08:00
#include "Exception.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
namespace Kylin {
Exception::Exception(const char *message) { init(nullptr, 0, message); }
Exception::Exception(const char *file, int line) { init(file, line, nullptr); }
Exception::Exception(const char *file, int line, const char *message) { init(file, line, message); }
Exception::Exception(const Exception &e) {
m_message = strdup(e.m_message);
m_location = strdup(e.m_location);
}
Exception &Exception::operator=(const Exception &e) {
if (this != &e) {
free(m_message);
free(m_location);
m_message = strdup(e.m_message);
m_location = strdup(e.m_location);
}
return *this;
}
const char *Exception::message() const { return m_message; }
const char *Exception::location() const { return m_location; }
void Exception::init(const char *file, int line, const char *message) {
m_message = message ? strdup(message) : nullptr;
if (file == nullptr) return;
char l[16] = {0};
#ifdef WIN32
itoa(line, l, 10);
#else
sprintf(l, "%d", line);
#endif
m_location = static_cast<char *>(malloc(strlen(file) + strlen(l) + 2));
if (m_location == nullptr) return;
strcpy(m_location, file);
strcat(m_location, ":");
strcat(m_location, l);
}
Exception::~Exception() {
free(m_message);
free(m_location);
}
ArithmeticException::~ArithmeticException() {}
NullPointerException::~NullPointerException() {}
IndexOutOfBoundsException::~IndexOutOfBoundsException() {}
NoEnoughMemoryException::~NoEnoughMemoryException() {}
InvalidParameterException::~InvalidParameterException() {}
InvalidOperationException::~InvalidOperationException() {}
} // namespace Kylin