add an exception program

this program messes with c++ exceptions.
This commit is contained in:
Fries 2023-06-21 17:01:33 -07:00
parent c27c685712
commit 3405208295
6 changed files with 144 additions and 2 deletions

16
.vscode/launch.json vendored Normal file
View file

@ -0,0 +1,16 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug",
"program": "${command:cmake.launchTargetPath}",
"args": [],
"cwd": "${workspaceFolder}",
}
]
}

View file

@ -1,4 +1,8 @@
{
"editor.detectIndentation": false,
"editor.insertSpaces": false,
"cmake.configureEnvironment": {
"CC": "clang",
"CXX": "clang++"
},
}

View file

@ -6,4 +6,5 @@ target_link_libraries(sqrt PRIVATE Math)
add_executable(inheritance inheritance.cc)
add_executable(enumerations enumerations.cc)
add_executable(namespaces namespaces.cc)
add_executable(functions functions.cc)
add_executable(FunctionArguments FunctionArguments.cc)
add_executable(exceptions exceptions.cc)

122
src/exceptions.cc Normal file
View file

@ -0,0 +1,122 @@
#include <exception>
#include <iostream>
#include <stdexcept>
namespace Crewmate {
enum class Type {
Imposter,
Crewmate
};
class NotImposterException : public std::exception {
private:
const char* message;
public:
NotImposterException(const char* msg)
: message(msg)
{
}
const char* what() const noexcept override
{
return message;
}
};
class NotCrewmateException : public std::exception {
private:
const char* message;
public:
NotCrewmateException(const char* msg)
: message(msg)
{
}
const char* what() const noexcept override
{
return message;
}
};
class Crewmate {
public:
Crewmate(Type type)
{
this->type = type;
}
void is_imposter()
{
switch (this->type) {
case Type::Imposter:
std::cout << "this crewmate is an imposter. sussy." << std::endl;
break;
case Type::Crewmate:
throw NotImposterException("this crewmate is not an imposter. not sussy.");
break;
}
}
void is_crewmate()
{
switch (this->type) {
case Type::Imposter:
throw NotCrewmateException("this crewmate is not a crewmate. sussy.");
break;
case Type::Crewmate:
std::cout << "this crewmate is a crewmate. not sussy." << std::endl;
break;
}
}
void invariant_testing()
{
switch (this->type) {
case Type::Imposter:
throw NotCrewmateException("this crewmate is not a crewmate. sussy.");
case Type::Crewmate:
throw NotImposterException("this crewmate is not an imposter. not sussy.");
break;
}
}
private:
Type type;
};
}
void exception_handler(const std::exception& exception, const std::string prefix = "Exception: ")
{
std::cerr << prefix << exception.what() << std::endl;
}
void invariant_test(Crewmate::Crewmate& crewmate)
{
try {
crewmate.invariant_testing();
} catch (const Crewmate::NotImposterException& err) {
exception_handler(err, "Imposter Exception: ");
} catch (const Crewmate::NotCrewmateException& err) {
exception_handler(err, "Crewmate Exception: ");
}
}
int main()
{
Crewmate::Crewmate imposter(Crewmate::Type::Imposter);
Crewmate::Crewmate crewmate(Crewmate::Type::Crewmate);
try {
imposter.is_imposter();
imposter.is_crewmate();
} catch (const Crewmate::NotCrewmateException& err) {
exception_handler(err);
}
try {
crewmate.is_crewmate();
crewmate.is_imposter();
} catch (const Crewmate::NotImposterException& err) {
exception_handler(err);
}
invariant_test(imposter);
invariant_test(crewmate);
}

View file

@ -11,5 +11,4 @@ namespace Imposter {
int main() {
Imposter::Namespace::sussy_function();
return 0;
}