try/catch compilation warning

C

ChrisB

Hello:

I notice that the following statements generate a "the variable 'e' is
declared but never used" warning:

try
{
Company.Fetch(CompanyID);
}
catch(RecordNotFoundException e)
{
return null;
}

I have no reason to use the variable e within my catch block and find this
warning annoying (my code is generating many such warnings). Is there a
commonly used way to avoid a compilation warning in this situation?

Thanks,
Chris
 
C

C# Learner

ChrisB said:
[...]
I have no reason to use the variable e within my catch block and find this
warning annoying (my code is generating many such warnings). Is there a
commonly used way to avoid a compilation warning in this situation?

In that case, use the following to simply filter the exception.

try
{
Company.Fetch(CompanyID);
}
catch(RecordNotFoundException)
{
return null;
}
 
M

Morten Wennevik

Hi ChrisB,

Sure is, change your catch block to

catch
{
return null;
}

Happy coding!
Morten Wennevik [C# MVP]
 
J

Jon Skeet [C# MVP]

ChrisB said:
I notice that the following statements generate a "the variable 'e' is
declared but never used" warning:

try
{
Company.Fetch(CompanyID);
}
catch(RecordNotFoundException e)
{
return null;
}

I have no reason to use the variable e within my catch block and find this
warning annoying (my code is generating many such warnings). Is there a
commonly used way to avoid a compilation warning in this situation?

Yes - don't declare the variable:

catch (RecordNotFoundException)
{
return null;
}
 
J

Jon Skeet [C# MVP]

Morten Wennevik said:
Sure is, change your catch block to

catch
{
return null;
}

That then catches *all* exceptions, however, which may very well not be
what is wanted.
 
C

ChrisB

Simple enough. Thanks!

C# Learner said:
ChrisB said:
[...]
I have no reason to use the variable e within my catch block and find this
warning annoying (my code is generating many such warnings). Is there a
commonly used way to avoid a compilation warning in this situation?

In that case, use the following to simply filter the exception.

try
{
Company.Fetch(CompanyID);
}
catch(RecordNotFoundException)
{
return null;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top