Hackerrank Inherited Code I'm back at it again with an Hackerrank challenge-Inherited Code . I just learnt exception and I thought it will be a great idea to get a second opinion on my implemented BadLengthException struct . 


  You inherited a piece of code that performs username validation for
  your company's website. The existing function works reasonably well,
  but it throws an exception when the username is too short. Upon
  review, you realize that nobody ever defined the exception.
  
  The inherited code is provided for you in the locked section of your
  editor. Complete the code so that, when an exception is thrown, it
  prints Too short: N (where \$N\$  is the length of the given
  username).
  
  Input Format
  
  The first line contains an integer, \$T\$ , the number of test cases. 
  Each of the \$T\$ subsequent lines describes a test case as a single
  username string, \$ U\$ .
  
  Constraints
  
  
  \$ 1 \leq T \leq 1000\$
  \$ 1 \leq |U| \leq 100\$
  The username consists only of uppercase and lowercase letters.
  
  
  Output Format
  
  You are not responsible for directly printing anything to stdout. If
  your code is correct, the locked stub code in your editor will print
  either Valid (if the username is valid), Invalid (if the username
  is invalid), or Too short: N (where \$N\$ is the length of the
  too-short username) on a new line for each test case.
  
  Sample Input

3
Peter
Me
Arxwwz

  
  Sample Output

Valid
Too short: 2
Invalid

  
  Explanation
  
  Username Me is too short because it only contains  characters, so your
  exception prints Too short: 2 .  All other validation is handled by
  the locked code in your editor.


#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;sstream&gt;
#include &lt;exception&gt;
using namespace std;

/* Define the exception here */
struct BadLengthException : public exception {
    int number;    
    public :
        BadLengthException(int n){
            number = n;            
        }

   const char * what () const throw () {
       std::stringstream ss;
       ss &lt;&lt; number;
       return ss.str(). c_str();

   }
};

bool checkUsername(string username) {
    bool isValid = true;
    int n = username.length();
    if(n &lt; 5) {
        throw BadLengthException(n);
    }
    for(int i = 0; i &lt; n-1; i++) {
        if(username[i] == 'w' &amp;&amp; username[i+1] == 'w') {
            isValid = false;
        }
    }
    return isValid;
}

int main() {
    int T; cin &gt;&gt; T;
    while(T--) {
        string username;
        cin &gt;&gt; username;
        try {
            bool isValid = checkUsername(username);
            if(isValid) {
                cout &lt;&lt; "Valid" &lt;&lt; '\n';
            } else {
                cout &lt;&lt; "Invalid" &lt;&lt; '\n';
            }
        } catch (BadLengthException e) {
            cout &lt;&lt; "Too short: " &lt;&lt; e.what() &lt;&lt; '\n';
        }
    }
    return 0;
}


Final Thoughts


I thought it was a great idea to used a struct as opposed class as a struct is for small grouping
Just like the previous question, I have no access to change  the headers,  main() and checkUsername() so I couldn't remove the using namespace std
This isn't really aimed at the core of your code and is more a reflection of the code challenge you're solving.

BadLengthException isn't a great name for the exception being represented.  It doesn't tell the caller that the name was too short (the exception could be used to represent a length that was too long).  The exception as you've implemented it has no why, so the exception itself doesn't contain sufficient information to tell the caller that it was too short / too long.  With that in mind, I would have been tempted to actually implement a TooShortException and then create typedef so that the supplied code still worked:

typedef TooShortException BadLengthException;