Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 577845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:11:50+00:00 2026-05-13T14:11:50+00:00

I have an old C++ project I made a while back. Well, it is

  • 0

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 throw and in my main loop I have something like this:

try{
    *(uint32_t*)&op_cache=ReadDword(cCS,eip);
    (this->*Opcodes[op_cache[0]])();
    eip=(uint16_t)eip+1;
}
catch(CpuInt_excp err){
    err.code&=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;
    }
}

And a simple opcode example(pulled out of thin air)

if(**regs16[AX]==0){
  throw CpuInt_excp(0); //throw divide by zero error
}

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)

Well, this being in the main loop, the try{}catch{} 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.

So! What would be the best way of replacing exceptions in this case? 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T14:11:50+00:00Added an answer on May 13, 2026 at 2:11 pm

    You haven’t shown your loop, but I’m guessing in pseudocode it’s:

    while (some_condition) {
        // Your try..catch block:
        try {
            // Do an op
        }
        catch (CpuInt_excp err) {
            // Handle the exception
        }
    }
    

    You could move the try..catch out a level:

    done = false;
    while (!done) {
        try {
            while (some_condition) {
                // Do an op
            }
            done = true;
        }
        catch (CpuInt_excp err) {
            // Handle the exception
        }
    }
    

    There I’ve included two loops, because I assume that if an exception occurs you want to be able to carry on (hard to tell without knowing what you’re doing in Int16, but I think you’re allowing carrying on after non-panic exceptions). Naturally if you don’t need to carry on, you only need one loop.

    The outer loop just restarts things after the exception. It can check the same condition as the inner loop if that condition is not expensive to check (e.g., it’s a program counter or something), or it can have a flag as in the above if the condition is expensive to check.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.