This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

simple try & catch

 
Pepo moussa
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • 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 "


 
Vishal Matere
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • 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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
 
Stevi Deter
Ranch Hand
Posts: 265
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
how about if i did this

 
Stevi Deter
Ranch Hand
Posts: 265
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48953
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and don't say if (invalidSize(size) == false) . . .

Say if (!invalideSize(size)) . . .
 
Stevi Deter
Ranch Hand
Posts: 265
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • 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
Java Mac Safari
  • Mark post as helpful
  • send pies
  • 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
  • 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 ..
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Pie
Posts: 48953
60
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
so any one can tell me how to make it throw exception ..?
 
Stevi Deter
Ranch Hand
Posts: 265
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic