D
Dmitriy Lapshin [C# / .NET MVP]
Hi all,
When a good developer should declare an exception variable in a catch
clause? Consider an example:
try
{
// Do something potentially dangerous...
}
catch(SomeException e)
{
// Handle the error.
}
My thinking is the "e" variable is only necessary when you need information
about the exception itself, like the error message, the stack trace and so
on. On the other hand, if you only need to do some simple action in the
error handler (say, return a default value), you don't need the "e"
variable - and the compiler would issue the "never used" varning on it if it
is declared.
Still, I have been taking a practice MCP test recently and it claims the
correct option is to always declare the exception variable - without any
reasonable explanation. Well, I fully realize that test authors are humans
who can make mistakes, but is it really a mistake, or I just misunderstand
something or have overlooked some Microsoft guidelines? (the MCP exams are
known to test heavily on anything Microsoft strongly recommends).
When a good developer should declare an exception variable in a catch
clause? Consider an example:
try
{
// Do something potentially dangerous...
}
catch(SomeException e)
{
// Handle the error.
}
My thinking is the "e" variable is only necessary when you need information
about the exception itself, like the error message, the stack trace and so
on. On the other hand, if you only need to do some simple action in the
error handler (say, return a default value), you don't need the "e"
variable - and the compiler would issue the "never used" varning on it if it
is declared.
Still, I have been taking a practice MCP test recently and it claims the
correct option is to always declare the exception variable - without any
reasonable explanation. Well, I fully realize that test authors are humans
who can make mistakes, but is it really a mistake, or I just misunderstand
something or have overlooked some Microsoft guidelines? (the MCP exams are
known to test heavily on anything Microsoft strongly recommends).