Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] Centralized Exception Handling with a Custom Exception Class in C++ #43

Closed
pedrobiqua opened this issue Jan 20, 2025 · 0 comments · Fixed by #45
Closed

[feat] Centralized Exception Handling with a Custom Exception Class in C++ #43

pedrobiqua opened this issue Jan 20, 2025 · 0 comments · Fixed by #45
Assignees
Labels
C++ enhancement New feature or request

Comments

@pedrobiqua
Copy link
Collaborator

Feature Request

Description

Introduce a centralized mechanism for handling project-specific exceptions by implementing a dedicated class in C++. This feature would enable consistent and organized management of errors, improving code maintainability, readability, and debugging efficiency.

This approach ensures that all exceptions in the project are managed in one place, reducing redundancy and the likelihood of inconsistent error handling.

Proposed Solution

Implement a class (e.g., CustomExceptions) in C++ that serves as the central location for defining and managing exceptions. The class would act as a base or container for derived exception classes, which would encapsulate specific error types relevant to the project.

Abstract Design

  1. Base Exception Class: Define a general-purpose exception class that inherits from std::exception. This class could include a constructor to accept custom error messages.
  2. Derived Classes: Create specific exceptions (e.g., InvalidInputException, FileNotFoundException) that inherit from the base exception class.
  3. Integration: Use these exceptions throughout the project to throw and catch errors in a structured way.
#include <exception>
#include <string>

class CustomExceptions {
public:
    class BaseException : public std::exception {
    protected:
        std::string message;
    public:
        explicit BaseException(const std::string& msg) : message(msg) {}
        virtual const char* what() const noexcept override {
            return message.c_str();
        }
    };

    // Example of a derived exception
    class InvalidInputException : public BaseException {
    public:
        explicit InvalidInputException(const std::string& msg)
            : BaseException("Invalid Input: " + msg) {}
    };

    class FileNotFoundException : public BaseException {
    public:
        explicit FileNotFoundException(const std::string& msg)
            : BaseException("File Not Found: " + msg) {}
    };

    // Additional specific exceptions can be added here.
};

Key Features:

  • Centralized error definitions.
  • Custom error messages with context.
  • Ability to extend the mechanism with new exception types as needed.

Alternatives Considered

  • Using std::exception Directly: This approach was considered but deemed less ideal as it does not provide sufficient context or customization for project-specific errors.
  • Adopting an External Library: While libraries like Boost.Exception could be used, they might introduce unnecessary complexity and dependencies.

Additional Context

This feature is part of a broader effort to improve error handling across the project. Centralizing exceptions can lead to more robust and maintainable code, especially in large or complex applications. Future extensions could include logging capabilities or error codes for enhanced debugging and tracing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C++ enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant