Avvenimenti
Mar 17, 11 PM - Mar 21, 11 PM
Ingħaqad mas-serje meetup biex tibni soluzzjonijiet skalabbli tal-IA bbażati fuq każijiet ta 'użu fid-dinja reali ma' żviluppaturi u esperti sħabi.
Irreġistra issaDan il-brawżer m'għadux appoġġjat.
Aġġorna għal Microsoft Edge biex tieħu vantaġġ mill-aħħar karatteristiċi, aġġornamenti tas-sigurtà, u appoġġ tekniku.
Property | Value |
---|---|
Rule ID | CA2201 |
Title | Do not raise reserved exception types |
Category | Usage |
Fix is breaking or non-breaking | Breaking |
Enabled by default in .NET 9 | No |
A method raises an exception type that's too general or that's reserved by the runtime.
The following exception types are too general to provide sufficient information to the user:
The following exception types are reserved and should be thrown only by the common language runtime:
Don't throw general exceptions
If you throw a general exception type, such as Exception or SystemException, in a library or framework, it forces consumers to catch all exceptions, including unknown exceptions that they don't know how to handle.
Instead, either throw a more derived type that already exists in the framework, or create your own type that derives from Exception.
Throw specific exceptions
The following table shows which exception to throw for various types of invalid arguments, including the value parameter in the set
accessor of a property.
Invalid argument | Exception |
---|---|
null reference |
ArgumentNullException |
Outside the allowed range of values (such as an index for a collection or list) | ArgumentOutOfRangeException |
Invalid enum value |
InvalidEnumArgumentException |
Contains a format that doesn't meet the parameter specifications of a method (such as the format string for ToString(String) ) |
FormatException |
Otherwise invalid | ArgumentException |
The following table shows which exception to throw for various types of invalid operations.
Invalid operation | Exception |
---|---|
Operation is invalid for the current state of an object. | InvalidOperationException |
Operation is performed on an object that has been disposed. | ObjectDisposedException |
Operation is not supported (such as in an overridden Stream.Write in a stream opened for reading). |
NotSupportedException |
Conversion would result in an overflow (such as in an explicit cast operator overload). | OverflowException |
For all other situations, consider creating your own type that derives from Exception and throw that.
To fix a violation of this rule, change the type of the thrown exception to a specific type that's not one of the reserved types.
Do not suppress a warning from this rule.
Feedback ta’ .NET
.NET huwa proġett b’sors miftuħ. Agħżel link biex tipprovdi l-feedback:
Avvenimenti
Mar 17, 11 PM - Mar 21, 11 PM
Ingħaqad mas-serje meetup biex tibni soluzzjonijiet skalabbli tal-IA bbażati fuq każijiet ta 'użu fid-dinja reali ma' żviluppaturi u esperti sħabi.
Irreġistra issa