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 3979074
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:07:31+00:00 2026-05-20T05:07:31+00:00

Just wondering, why is it better to throw a class over an enum Surely

  • 0

Just wondering, why is it better to throw a class over an enum

Surely throwing classes is more overhead?

e.g.

enum MyException
{
   except_a,
   except_b,
   except_c
}


void function f(){
  throw except_a;
}


int main(int arc, char* argv[]){
  
  try{

  } catch (MyException e){
    switch(e){
      except_a: break;
      except_b: break;
      except_c: break;
    }
  }

  return 0;
}

Apart from the overhead. I also need to declare a class for each one which might override std::exception or something. More code, larger binary… what’s the benefit?

  • 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-20T05:07:31+00:00Added an answer on May 20, 2026 at 5:07 am

    Given

    enum MyException
    {
       except_a,
       except_b,
       except_c
    }
    

    write a catch clause that only catches except_c exceptions.

    With

    struct my_except {};
    struct my_except_a : my_except {};
    struct my_except_b : my_except {};
    struct my_except_c : my_except {};
    

    that’s easy, since you can catch the base class or derived classes.

    Many of the common advantages of derivation apply to exceptions. For example, base classed can stand in for derived classes and code needs to know only base classes to deal with derived exceptions. That’s a form of polymorphism, while the enum is a switch over a type.

    The general rule about polymorphism applies here, too: Whenever you are tempted to use a switch over a type, you are dismissing the advantages of polymorphism. The problems you are getting yourself into will be seen once the code has expanded to hundreds of kLoC, and you need to add a new type. With polymorphism, that’s easy, because most code will be fine dealing with base class references only.
    With the type enum, you have to hunt down every single switch statement over that enum and check whether you need to adapt it.

    Things like these have killed more than one company.


    Here’s an afterthought: When they’ve done this for a while, users usually start to add all kinds of data to their exception types. A classic is to take __FILE__ and __LINE__ in an exception’s constructor to be able to see where an exception came from. But this, too, needs exceptions to be class types.

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

Sidebar

Related Questions

gcc 4.4.1 I am just wondering which standard is better and more portable? I
I'm just wondering which one is better, or more recommended for processing form data.
Just wondering if there is a better way to write the following PL/SQL piece
I'm just wondering if there's a better way of doing this in SQL Server
I'm just wondering if it exists better solution for this. BitConverter.ToInt32(sample_guid.ToByteArray(), 0)
Just wondering if there is a better way to write this CSS? It repeats
Was just wondering, which way is better to fully initialise an object: NSString *myString
I am just wondering would it be better to do this: if((fd = open(filename,
I was just wondering which approach is better. Creating a complex select query with
gcc (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1 c89 Just wondering is there a better way to do

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.