Re: catch (Exception e)
- From: "Isak Savo" <isak savo gmail com>
 
- To: dashboard-hackers gnome org
 
- Subject: Re: catch (Exception e)
 
- Date: Tue, 24 Oct 2006 14:47:30 -0400
 
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]