• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

simple try & catch

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am not sure if this is part of code is right or wrong ! so if any one can tell me ? " the Try and catch part "


 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by I moussa:
i am not sure if this is part of code is right or wrong ! so if any one can tell me ? " the Try and catch part "





I added IllegalArgumentException in else part because in earlier code IllegalArgumentException was not getting thrown ATALL from try block. Hence I made sure that when Arguments are incorrect throw new exception. IllegalArgumentException is runtime exception.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's close, but you will need some code to actually throw the exception.
 
Vishal Matere
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by marc weber:
It's close, but you will need some code to actually throw the exception.





Will not this throw Exception?
 
Pepo moussa
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i know how to throw the exception using if & else ..that what i originally had" i find it easier" but for this project its a must to use the try catch .. so any ideas
 
Ranch Hand
Posts: 265
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I Moussa,

The code in the try block you initially posted will never actually throw an IllegalArgumentException.

This means the try/catch statement as written really has no utility.

Also, it's generally poor practice to catch an exception and throw a new version of the same exception. This loses all the information about where there initial exception was thrown.

If you need to add additional information to the Exception, then chain the exception (throw new Exception("why i caught it here",originalException)).

If you aren't going to do anything with the exception, then you should probably just specify it in the method statement and let it get thrown up the chain.
 
Pepo moussa
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how about if i did this

 
Stevi Deter
Ranch Hand
Posts: 265
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I Moussa,

Did you try compiling that code?

You cannot nest catch statements that way. And why would you want to?

I do think you're getting a little closer overall to understanding how to use exception throwing and catching. Spend some time experimenting with writing code and compiling it and seeing what happens.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and don't say if (invalidSize(size) == false) . . .

Say if (!invalideSize(size)) . . .
 
Stevi Deter
Ranch Hand
Posts: 265
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Personally, I find



to be far more difficult to read than



It's so easy to miss the "!".

Sometimes, it's better to refactor as



And then there are the times I wish we had Perl's unless.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vishal Matere:
... Will not this throw Exception?


Sorry, my response was to the original poster. I did not see your reply until after I had posted.
 
Pepo moussa
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


thanks Campbell Ritchie for the advise , i do know it already but my prof takes points off because some thing like this if (!invalideSize(size)) is not clear enough:S,
Stevi Deter well i thought so that nested catches wont work, i never actually had to do nested catches before so i am just trying to get the best of it ..
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You still have the problem that nothing in the try block will ever throw an IllegalRoomException, so your catch block will never run.
[ May 01, 2008: Message edited by: Joanne Neal ]
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Stevi Deter:
Sometimes, it's better to refactor as

What a good idea!
 
Pepo moussa
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so any one can tell me how to make it throw exception ..?
 
Stevi Deter
Ranch Hand
Posts: 265
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I Moussa,

I'd recommend you review the Java Tutorial on Exceptions at this point.

You're very close, I think reviewing the basic concepts again will help you see what you need to do.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic