← 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= newline. The output ends with a newline — print one after the last line.
Hints4 available
Sign in to use hintsTopics:
ClassesExceptions
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · 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.