How should try-catch blocks be placed when your code uses OpenMP tasks? Should a try-catch block be defined inside each task or can a single try-catch block encompass all tasks? In other words consider this code:
int main()
{
#pragma omp parallel
{
#pragma omp single
{
try
{
#pragma omp task
{ /*some code here*/ }
#pragma omp task
{ /*some code here*/ }
}
catch(Exception &e)
{
//I want to do something here whenever one of the tasks throws an exception
}
}
}
}
Is the above code correct if I want to catch any exception thrown by one of the tasks, in order to lets say print an error message corresponding to the exception and abort the program? Or do I need to define a try-catch block inside each task, like this?:
int main()
{
#pragma omp parallel
{
#pragma omp single
{
#pragma omp task
{
try
{
/*some code here*/
}
catch(Exception &e)
{
//print an error message here if there is an exception in the task and abort the program
}
}
#pragma omp task
{
try
{
/*some code here*/
}
catch(Exception &e)
{
//print an error message here if there is an exception in the task and abort the program
}
}
}
}
}
Or do I need a try-catch block inside each task but also surrounding all the tasks?