title: Best way of replacing heavy exception use?
body: <p>I have an old C++ project I made a while back. Well, it is a CPU emulator. Whenever a CPU fault happens(such as divide by zero, or debug breakpoint interrupt, etc) in my code it just does a <code>throw</code> and in my main loop I have something like this:</p>
  
  <pre><code>try{
      *(uint32_t*)&amp;op_cache=ReadDword(cCS,eip);
      (this-&gt;*Opcodes[op_cache[0]])();
      eip=(uint16_t)eip+1;
  }
  catch(CpuInt_excp err){
      err.code&amp;=0x00FF;
      switch(err.code){
          case 0:
          case 1: //.....
          Int16(err.code);
          break;
          default:
          throw CpuPanic_excp("16bit Faults",(err.code|0xF000)|TRIPLE_FAULT_EXCP);
          break;
      }
  }
  </code></pre>
  
  <p>And a simple opcode example(pulled out of thin air)</p>
  
  <pre><code>if(**regs16[AX]==0){
    throw CpuInt_excp(0); //throw divide by zero error
  }
  </code></pre>
  
  <p>What this code basically does is just reads an opcode and if an exception occurred, then call the appropriate interrupt(in the CPU, which just changes the EIP) </p>
  
  <p>Well, this being in the main loop, the <code>try{}catch{}</code> overhead really adds up. This isn't a premature optimization, I profiled it and gcc's exception helper functions(without even doing any faults and thus no throws) and the helper functions took up over 10% of the total execution time of a long running emulated program. </p>
  
  <p>So! <strong>What would be the best way of replacing exceptions in this case?</strong> I would prefer not to have to keep track of return values because I already have a ton of code written, and because keeping track of them is really difficult when functions get really deep. </p>