Main Content

MISRA C++:2008 Rule 15-0-3

Control shall not be transferred into a try or catch block using a goto or a switch statement

Description

Rule Definition

Control shall not be transferred into a try or catch block using a goto or a switch statement.

Rationale

Transferring control into a try or catch block by using a goto or a switch statement results in ill-formed code that is difficult to understand. The intended behavior of such code is difficult to identify and the code might result in unexpected behavior. Abruptly entering into an exception handling block might cause compilation failure in some compilers while other compilers might not diagnose the issue. To improve code understanding and reduce unexpected behavior, avoid transferring control into a try or a catch block.

Polyspace Implementation

Polyspace® flags the goto and switch statements that jump into a try or a catch block.

Troubleshooting

If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

#include<cstdint>
void foo ( int32_t input )
{
	if ( input==1 )
	{
		goto Label_1; // Noncompliant
	}
	if ( input==2 )
	{
		goto Label_2; // Noncompliant
	}
	switch ( input ) //Noncompliant
	{
	case 1:
		try
		{
			Label_1:
		case 2: 
			break;
		}
		catch ( ... )
		{
			Label_2:
		case 3: 
			break;
		}
		break;
	default:
		{
			//...
			break;
		}
	}
}

In this example, goto and switch statements are used to jump into a try-catch block. Jumping into a try-catch block makes the code difficult to understand. Abrupt transfer of control into a try block or a catch block might result in compilation failure. Polyspace flags the goto and switch statements.

Check Information

Group: Exception Handling
Category: Required

Version History

Introduced in R2013b