โ Back to Classes
Question 153
Custom Exception Class
Custom Exception Class
Create a custom exception classValidationErrorthat inherits fromstd::exception, then use it in a function. Requirements forValidationError: 1. Privatestd::string messagefield 2. ConstructorValidationError(std::string msg): stores the message 3. Overridewhat() const noexceptto returnmessage.c_str()Also write a functionvalidateUsername(const std::string& name): - Ifnameis empty, throwValidationError("Username cannot be empty")- Ifname.length() < 3, throwValidationError("Username too short")- Otherwise returnnameDo NOT write a main function.
Expected Output:
alice
Topics:
ClassesExceptions
Code Editor
1
Tab to indent ยท Ctrl+Enter to run ยท Ctrl+Space to expand shortcuts (cout, fori)
Your Output
Run your code to see the output here...
Test Cases
Run your code to see test case results.