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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:55:46+00:00 2026-06-15T09:55:46+00:00

I’m wondering if it is safe to throw an C++ exception inside an OMP

  • 0

I’m wondering if it is safe to throw an C++ exception inside an OMP critical section.

#pragma omp critical (my_critical_section)
{
    ...
    throw my_exception("failed")
    ...       
}

g++ does not complain. I’m puzzled because it complains about return statements inside the critical section. It returns the error: invalid exit from OpenMP structured block when I write

#pragma omp critical (my_critical_section)
{
    ...
    return;
    ...       
}

So, why is it OK to leave the critical section with an exception, but it is not OK to leave it with a return statement?

  • 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-06-15T09:55:48+00:00Added an answer on June 15, 2026 at 9:55 am

    No, it is not OK to leave a critical section with exceptions. g++ does not complain in this case but it silently inserts an implicit try/catch around the block of the critical section. For example the following code:

    #pragma omp critical (my_crit)
    {
       throw 3;
    }
    

    gets lowered by the OpenMP processor of GCC 4.7 into:

    #pragma omp critical (my_crit)
    __builtin_GOMP_critical_name_start (&.gomp_critical_user_my_crit);
    try
      {
        D.20639 = __cxa_allocate_exception (4);
        try
          {
            MEM[(int *)D.20639] = 3;
          }
        catch
          {
            __cxa_free_exception (D.20639);
          }
        __cxa_throw (D.20639, &_ZTIi, 0B);
      }
    catch
      {
        <<<eh_must_not_throw (terminate)>>>
      }
    __builtin_GOMP_critical_name_end (&.gomp_critical_user_my_crit);
    

    Reaching the implicit built-in catch-all handler <<<eh_must_not_throw (terminate)>>> results in quite ungraceful termination:

    terminate called after throwing an instance of 'int'
    Abort trap: 6
    

    The implicit try/catch is inserted regardless of the presence of an outer try/catch construct, i.e. the exception would never ever leave the critical section.

    The OpenMP standard mandates, that if an exception is thrown within most OpenMP constructs (parallel, section, master, single, for, critical, task, etc.), execution must resume within the same construct and that the same thread must catch the exception. Violation of this restriction leads to non-conformant OpenMP code and g++ simply enforces the conformance by inserting try/catch blocks with termination handlers inside all such constructs.

    As for the error when a return statement is present, OpenMP defines a strcutured block in C/C++ as:

    For C/C++, an executable statement, possibly compound, with a single entry at the top and a single exit at the bottom, or an OpenMP construct.

    and also (for all languages):

    The point of exit cannot be a branch out of the structured block.

    Obviously return constitutes a branch ouf of the block, different from simply falling of the bottom of the block.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Does anyone know how can I replace this 2 symbol below from the string
I need a function that will clean a strings' special characters. I do NOT
I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

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.