This X++ Code Snippet describes how to handle all values in a switch statement or if else statement. If you want to avoid someone calls the function or method with a value which isn’t explicitly handled in your conditional statement, you can throw an error with the name of the function to the end user, making the end user aware of the fact that an unexpected value is used in the written code.
For a switch statement you add the default section as follows:
switch (_variable)
{
case value1:
some code;
break;
case value2:
some code;
break;
default:
throw error(strFmt("@SYS23396",funcName()));
}
For an if else statement:
if (value1)
{
some code;
}
else
{
throw error(strFmt("@SYS23396",funcName()));
}
Post a Comment