Keren S

Hello!

Quit a few times in my code I catch an exception parameter that I have nothing to do with:

try

{

}

Catch (Exception e)

{

DisplayErrorMsg("Failed...");

}

The problem is that when compling, every such line creates a warning "The variable e is declared but never used". How do I get rid of those warning messages

I tried to use write:

Catch

{

DisplayErrorMsg("Failed...");

}

But it seems that if I do that, the execption gets automaticaly thrown upwards which I don't want it to do since i already handled it.

Thank you in advance,

Keren



Re: Visual C# Language Catch (Exception e) - Get rid of warning

Matthew Watson

Do this:

try

{

}

Catch (Exception)

{

DisplayErrorMsg("Failed...");

}







Re: Visual C# Language Catch (Exception e) - Get rid of warning

Keren S

I don't get it! I was sure that I already tried this variant but apperntly I didn't. It works indeed!

Thank you very much for our help.

Keren





Re: Visual C# Language Catch (Exception e) - Get rid of warning

Sonik1

There are two ways.

1.

try

{

}

Catch (Exception e)

{

e=e;

DisplayErrorMsg("Failed...");

}

2.

try

{

}

Catch (Exception)

{

DisplayErrorMsg("Failed...");

}






Re: Visual C# Language Catch (Exception e) - Get rid of warning

ShellShock

Sonik1 wrote:

There are two ways.

1.

try

{

}

Catch (Exception e)

{

e=e;

DisplayErrorMsg("Failed...");

}

2.

try

{

}

Catch (Exception)

{

DisplayErrorMsg("Failed...");

}

Except I would not advise anyone to use your first option, which only leaves one way.





Re: Visual C# Language Catch (Exception e) - Get rid of warning

marco.ragogna

in any case, I would always store in some place the complete exception error (EventLog, local log file, etc.), so in that case you will use e and you will not get the error anymore.



Re: Visual C# Language Catch (Exception e) - Get rid of warning

Keren S

Why is that Why would you ALWAYS log it There are planty of cases when it is a total waist.

For example, an easy way to check an input is to try and convert it to the format you expect. If you except a Decimal for example, all you should on "Save" is try to convert the input to Decimal (which you would do anyway) and catch on the suitable exception.

No Exception: it was a legal input and you can continue saving. Exception: You notify the user that the input is not correct and allow him to correct his input. Why would you log it in that case It is totaly not interessting.





Re: Visual C# Language Catch (Exception e) - Get rid of warning

Robert McNeal, EMCS, Inc.

What you are doing here certainly works but...

It flies in the face of EXCEPTION handling. Essentially, you are not catching exceptions, but handling validation. Exception handling, by definition, is handling UKNOWN errors in the code, not know problems that can be avoided. There is a cleaner and more efficient way to check types than to use a try...catch block. Try...catch blocks add overhead that is unneccessary if all you want to do is ensure that the type you passed is of type decimal.





Re: Visual C# Language Catch (Exception e) - Get rid of warning

Matthew Watson

That's probably true nowadays, because we have things like double.TryParse().

In .Net 1.x, however, using double.Parse() inside a try/catch block was the only sensible way to parse doubles (and other things like Datetime).





Re: Visual C# Language Catch (Exception e) - Get rid of warning

IsshouFuuraibou

It should also be noted, that if you catch Exception and then drop it (not rethrow it), you are claiming to know how to handle every Exception that is or will ever be created if it occurs inside that try/catch block. This includes any derived Exceptions thrown by the CLR, InterOp, User code, etc. There are some you just can't log and continue like StackOverflowException, OutOfMemoryException, and ExecutionEngineException. This may be example code, but it's an important part of Exception handling.

It is usually best to do as many checks that you can do before hand than to let it Exception out. Checks are far cheaper than throwing and handling an Exception.

There are plenty of exceptions that you can know how to handle, and thus don't need to log. However exceptions, which are unexpected or unknown errors that happen, are more likely not to be handled. Thus logging it somewhere gives you information about how and where (stack trace) an exception happened in your application.





Re: Visual C# Language Catch (Exception e) - Get rid of warning

Keren S

Aha.... So an Exception should be UNKNOW

Is that why there are exceptions such as: CannotRemoveControlException, ControlNameAlreadyExistsException, Excel.SetDataBindingFailedException, CantStartSingleInstanceException, NoStartupFormException

What exactly is unknown in any of those exception if you catch them specifically

Would anyone ever come up with such exeptions that you are able to catch specifically if they intended the exception to be unknow





Re: Visual C# Language Catch (Exception e) - Get rid of warning

Keren S

Dear Erik!

You are totally missing the point. It isn't at all that I am claiming to know how to handle every exception.

I simply don't care what it is. It is simply to accept it: In most cases the e text is simply not interresting! (as long as we are talking about a WORKING application that has hardly any bugs and not during the time you build it). Allow me to exeplain:

To begin with, I am absoluty not interseted in showing the user the text that comes with the exception because it is too technic. I just want to inform them that something went wrong and in what part did it go wrong (saving to db, writing to file, etc...).

In a worked and tested system it is very very seldom that the exception text is usfull:

Either you can reason what is wrong yourself in less time than what it would take you to go into the log file and find it, because you know that the system is working so it must have been something external that happened (like something being offline for example);

or that the text is not specifide enough in order to be of much help (the person who wrote those exceptions couldn't foresee all possible problems either so the texts are somewhat general).

In the very very few cases that something goes wrong in an exception level (which I have only experianced once although I am working with quite a few applications and that was because a WebService was down) I just go in with the debugger, do the same my self and see what is wrong/read the e message).

If you all are that depended on exceptions it probably measn that you did not debug and test your applications enough! :-)

But all thsi is vased on my needs! My applications are not that big so I can test and debug them thorowly and all my users sit in the same house as I am and would call support as soon as they would see an error message anyway. So it is more affective to avoid logging. The time that bulding a proper logging takes is exponetionaly greater than the times I would have any use of it.

However, in cases where it is a batch job or users that are spread all over the country , than I do have logging and than I use the e message anyway.

Keren





Re: Visual C# Language Catch (Exception e) - Get rid of warning

Matthew Watson

Who is "Erik"

Anyways, I think you misundertand:

If you do "catch (System.Exception)" then your code doesn't know the actual kind of exception, and it has just handled an "unknown exception".

If code ignores such exceptions (does no logging and does not rethrow the exception) then it could be covering up a serious bug.

This is ok for in-house programs, I guess, but for industrial-strength commercial programs, doing that is completely unacceptable.




Re: Visual C# Language Catch (Exception e) - Get rid of warning

Robert McNeal, EMCS, Inc.

Precisely what I meant Matthew.

It is not that ALL exceptions are "unknown", it is that, IMHO, exceptions by definition should be exceptional, pardon the pun.

IOW, you should handle any forseeable problems like trying to open a file that does not exist by checking for the existence of the file first.

Then deal with true exceptions using try...catch scenarios.

Like Matthew said, if you just swallow any exception without logging, or something, you'll never know if you have a bug or not.





Re: Visual C# Language Catch (Exception e) - Get rid of warning

Keren S

Hej igen!

Erik was another guy that replied (see answers above, one of them is sign with the name Erik). This answer was menat for him

Of course my question was layed out geberally since the problem applies to Execptions generally and not to a specific exception.

However I never said that I ignore an exeception. I NEVER Ignore an exception.

The only thing I do is ignore the parameter. I either throw it up, notify the user that it went wrong or handle it in another way in case I know what that if there is a certain exception here than it is because xyz.

I never said I ignore exceptions! Only the exception massage.

Keren