Skip to content

BSL255 — Cast to number of try catch block

Summary

Cast to number of try catch block

Identifiers

Field Value
Rule code BSL255
Compatible alias TryNumber
Severity WARNING
Enabled by default Yes
Implemented Yes
Tags error-handling, suspicious

Behavior

  • The public identifier BSL255 and alias TryNumber are stable.
  • The rule reports the cases documented on this page.
  • Suppressions and project configuration are applied before publication.
  • The rule requires neither an external analyzer nor network access.

Configuration and suppression

BSL### is the primary stable identifier. The compatible alias is accepted in select, ignore, and compatible block suppression comments.

[tool.onec-hbk-bsl]
select = ["BSL255"]
ignore = ["TryNumber"]

All three suppression families support both a current line and a range. When an opening comment follows code, it affects only that line. Use any one form:

  • noqa:
Value = "example";  // noqa: BSL255
  • bsl-disable:
Value = "example";  // bsl-disable: BSL255
  • compatible BSLLS form:
Value = "example";  // BSLLS:TryNumber-off

When the same opening comment is on a line by itself, it starts a range. Close it with the matching marker from the same family:

// noqa: BSL255
// code without this diagnostic
// noqa-enable: BSL255

// bsl-disable: BSL255
// code without this diagnostic
// bsl-enable: BSL255

// BSLLS:TryNumber-off
// code without this diagnostic
// BSLLS:TryNumber-on

To disable the rule until the end of the file, omit the closing noqa-enable, bsl-enable, or BSLLS:…-on marker.

Opening and closing markers must belong to the same family.

Description

It is incorrect to use exceptions to cast a value to a type. For such operations, it is necessary to use the capabilities of the TypeDescription object.

Examples

Incorrect:

Try
 NumberDaysAllowance = Number(Value);
Raise
 NumberDaysAllowance = 0; // default value
EndTry;

Correct:

TypeDescription = New TypeDescription("Number");
NumberDaysAllowance = TypeDescription.CastValue(Value);

Sources