Menu

catchExceptionByValue / exceptRethrowCopy and Fundamental types

2024-05-03
2024-05-03
  • Yannick HOUDOT

    Yannick HOUDOT - 2024-05-03

    I agree that exception should by caught by reference and not re-thrown as a copy ... in general.
    But when the exception is a fundamental types like an enum or a int... it costs less than a reference.

    I was wondering if an exception could be made in a case of enum, integers, or any simple type with a size smaller than a pointer ?

     
  • CHR

    CHR - 2024-05-03

    Can you share some example code? This is clean:

    enum E { E0 };
    int f() {
        try {
            throw E0;
        } catch (E e) {
            return e;
        }
        return 1;
    }
    
     
  • Yannick HOUDOT

    Yannick HOUDOT - 2024-05-03

    ok, my bad...
    in my analyse, cppcheck didn't have access to the definition of the enum E (in your example).

    all is good, sorry for the noise.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.