• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 481
  • Last Modified:

Exception handling in C#

Hello,

When I try to raise exceptions like the one below

                              if (reader.FieldCount < 2)
            {
                  ReportError(new Exception("should select atleast 2"),"");
                  return;
            }

I am getting an errror saying "Do not raise reserved exception types"

I can I handle this. pls help
0
sureshraina
Asked:
sureshraina
  • 2
1 Solution
 
aki4uCommented:
try this:

throw(new Exception("Test"));
0
 
naveenkohliCommented:
Applications should be using ApplicationException object.
0
 
sureshrainaAuthor Commented:
The following exception types are too general to provide sufficient information to the user:

System.Exception
System.ApplicationException
System.SystemException
The following exception types are reserved and should be thrown only by the common language runtime:

System.ExecutionEngineException
System.IndexOutOfRangeException
System.NullReferenceException
System.OutOfMemoryException
How to Fix Violations
To fix a violation of this rule, change the type of the thrown exception to a specific type that is not one of the reserved types.


How do I fix this even after using applicationexception object it is throwing hte same eroro
0
 
naveenkohliCommented:
As it is being suggedted by your coding rules... you should be using one of the specific execption types and not the general type exception.
I guess in your case, since argument value range is wrong you can throw something like.

throw new ArgumentException(".....");
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now