Decoding assembly instructions in a Game Boy disassembler I am coding a game boy disassembler in C++. The program (simplified here) converts a vector of byte into an assembly instruction and print it. 

To do so, it iterates through the vector of bytes (char) with an iterator. I noticed that with the it!=char_vect.end() condition, there is the risk that the iterator goes out from the vector. So I have to check that I do not get out of the vector at each iteration and if it occurs, I throw an error.

#include &lt;iostream&gt;
#include &lt;vector&gt; 

int disassemble(std::vector&lt;char&gt;::iterator&amp; it){

    int opcode = *it;
    int opbytes = 1; //number of bytes used by the operator

    switch(opcode){
        case 0x76 : std::cout&lt;&lt;"HALT "&lt;&lt;std::endl; opbytes = 1; break;
        case 0x10 : std::cout&lt;&lt;"STOP $"&lt;&lt;(int)*(it+1)&lt;&lt;std::endl; opbytes =2; break; 
        //... About 250 different cases
        default : std::cout&lt;&lt;"Instruction not implemented yet"&lt;&lt;std::endl;
    }

    return opbytes;
}

int main(){

    std::vector&lt;char&gt; char_vect = {0x76, 0x10, 0x20}; // Fill vector of char
    std::vector&lt;char&gt;::iterator it = char_vect.begin();

    // First I only did that
    //while (it!=char_vect.end()){
    //  it+=disassemble(it);
    //}

    // But I have to check that I do not go out of the char_vect

    while (it!=char_vect.end()){
        try{
            it+=disassemble(it);
            //check that it do not overpass end
            if (size_t(it - char_vect.begin())&gt;char_vect.size()){
                throw "Try to disassemble instruction outside of memory bounds";
            }
        }
        catch(const char * c){
            std::cerr &lt;&lt; "Fatal error: " &lt;&lt; c &lt;&lt; std::endl;
            return 1;
        }
    }
    return 1;
}


This code works fine. Now I wonder, I have a possible exception case which is the iterator going further than vect_char.end(). Is a try / catch an appropriate way to handle this case knowing that:


The reason for the iterator to go out of bounds could be either:


A non valid input byte sequence, for example {0x76, 0x20, 0x10} since the instruction 0x10 expects an argument. This should not append.
A mistake in the disassemble function. For exemple with the valid input {0x76, 0x10, 0x20}, if I code by mistake that 0x10 uses 3 opbytes instead of 2, the iterator will go out of bounds. This should not append either.

If I iterated through the vect_char with an index, I would not have this issue and the code would be more compact


I never really used try/catch before and I do not know if it is intented for such situations, so I would like to know, how would you have handled this unexpected case?
I have some ideas about how you might be able to improve your program.

Avoid problems

Rather than trying to deal with the problem for every instruction, one approach is avoiding it entirely.  One way to do that is to simply append a number of bytes to the end of the vector.  If the maximum bytes for an instruction is \$n\$, then append \$n-1\$ bytes to the end and stop when you've advanced into the padded area.  

Check before advancing

One could also pass the number of remaining bytes to the disassemble function.  However, the mechanism I'd suggest would be to pass a range, e.g.:

int diassembleOne(std::vector&lt;char&gt;::iterator&amp; it, std::vector&lt;char&gt;::iterator&amp; end) {
    //  figure out number of bytes for this opcode
    if (std::distance(it, end) &gt; opbytes) {
        it = end;
        // throw or return 0
    }
    // disassemble the instruction thoroughly
    std::advance(it, opbytes);
    return opbytes;
}


Use const iterators

If all the code is doing is disassembling, then it shouldn't alter the underlying vector.  For that reason, I'd recommend passing a std::vector&lt;char&gt;::const_iterator &amp;.

Use classes

I'd suggest using an Opcode class like this:

class Opcode {
    char code;
    short int bytes;
    std::string_view name;
    bool operator==(char opcode) const { return code == opcode; }
    int decode(std::vector&lt;char&gt;::const_iterator&amp; it, std::ostream&amp; out=std::cout) const {
        out &lt;&lt; name; 
        ++it;
        for (int i{bytes-1}; i; --i) {
            out &lt;&lt; static_cast&lt;unsigned&gt;(*it++);
        }
        out &lt;&lt; '\n';
        return bytes;
    }
};

constexpr std::array&lt;Opcode,2&gt; instructions {
    { 0x10, 2, "STOP $" },
    { 0x76, 2, "HALT " },
};


Pass a pair of iterators to the dissemble function

As mentioned before, you can pass a pair of iterators to the disassemble function.  Using that plus the class above:

int disassembleOne(std::vector&lt;char&gt;::const_iterator&amp; it, std::vector&lt;char&gt;::const_iterator&amp; end){
    auto op{std::find(instructions.begin(), instructions.end(), *it)};
    if (op == instructions.end()) {
        std::cout &lt;&lt; "Instruction not found\n";
        it = end;
        return 0; // instruction not found or off the end of the passed array
    }
    if (std::distance(it, end) &lt; op-&gt;bytes) {
        std::cout &lt;&lt; "Not enough bytes left to decode " &lt;&lt; op-&gt;name &lt;&lt; '\n';
        it = end;
        return 0; // instruction not found or off the end of the passed array
    }
    return op-&gt;decode(it);
}


Now main becomes very simple:

int main(){
    const std::vector&lt;char&gt; char_vect = {0x76, 0x10, 0x20, 0x10}; // Fill vector of char
    auto end{char_vect.cend()};

    for (auto it{char_vect.cbegin()}; it != end; disassembleOne(it, end)) {
    }
}


Another way to do this would be to put more of the processing in the Opcode itself -- it would make sense that each opcode would know how to decode itself.

Be clear about caller expectations

This code, as with the original code, expects that the it being passed is a valid iterator that is not the end.  It is good to document that in comments in the code.