• Post Reply Bookmark Topic Watch Topic
  • New Topic

Try and Catch  RSS feed

 
Ale Lima
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a doubit !

If I have a method and I used the trhows for 2 execptions, like this.



In this example, is there same convection, where I need to put try/catch ?

I think the friendly way is ....


Am I correct ??


 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are close. But instead of "new thow MXEception();" you would use "throw new MXException();"

OR

Instead of creating a new exception, you could just toss the one you caught. Like this:

 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or just don't catch them at all, letting them be thrown automatically.
 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob takes the shot... nothing but net

I always use try/catch though to make maintenance easier and make it more easily understood.
 
Pushkar Choudhary
Rancher
Posts: 425
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it also depends on whether you want to do anything specific before throwing the exception, like logging it or throwing it as a different exception.

If you want to log the exception or do some other computation or even catch it and throw it back as a different exception, then it makes sense to enclose the code in a try-catch block. Otherwise, if you don't want to do anything with the exception, then you can let it be thrown automatically using the throws.


-Pushkar
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pushkar Choudhary wrote:I think it also depends on whether you want to do anything specific before throwing the exception, like logging it or throwing it as a different exception.

If you want to log the exception or do some other computation or even catch it and throw it back as a different exception, then it makes sense to enclose the code in a try-catch block. Otherwise, if you don't want to do anything with the exception, then you can let it be thrown automatically using the throws.


-Pushkar
Agree. you should think: Do you think this method is the best place to do something about an Exception, or should another method do something? Of course you might get different answers for different Exceptions.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!