Re: catch (Exception e)



2006/10/24, Max <mwiehle2 ix urz uni-heidelberg de>:
[...]

        try {...}
        catch (Exception e) {
                Logger.Log.Debug ("Something went wrong...");
        }

IIRC c# also allows:

        catch ()
or
        catch (MyOwnException)

without defining a variable if it is not needed.

Is there any reason for always putting the e or the ex variables there
even though they are not used afterwards?

The answer to your question is no; there is no need to declare a
variable name unless you're going to use it (hence the warning).

So:
   catch (MyException ex) { /* Do stuff */ }
is the same as
   catch (MyException) { /* Do stuff */ }
except that you can't use the exception info inside the  { } block.

To catch everything, you'd typically write
   catch { /* Do stuff */ }
which is identical to:
   catch (Exception) { /* Do stuff */  }

Although, as Daniel Naber pointed out in another reply, it's probably
better to declare the variable and then do something useful with it,
such as print it. :-)

Isak



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]