Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes simple try & catch Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "simple try & catch " Watch "simple try & catch " New topic
Author

simple try & catch

Pepo moussa
Ranch Hand

Joined: Mar 20, 2008
Posts: 36
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

Joined: Jan 22, 2008
Posts: 81
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.


SCJP <br />SCWCD <br />SCBCD <br />SCEA-1
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

It's close, but you will need some code to actually throw the exception.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Vishal Matere
Ranch Hand

Joined: Jan 22, 2008
Posts: 81
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

Joined: Mar 20, 2008
Posts: 36
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

Joined: Mar 22, 2008
Posts: 265

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.


There will always be people who are ahead of the curve, and people who are behind the curve. But knowledge moves the curve. --Bill James
Pepo moussa
Ranch Hand

Joined: Mar 20, 2008
Posts: 36
how about if i did this

Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265

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

Joined: Oct 13, 2005
Posts: 38007
    
  22
. . . and don't say if (invalidSize(size) == false) . . .

Say if (!invalideSize(size)) . . .
Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265

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

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Mar 20, 2008
Posts: 36


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

Joined: Aug 05, 2005
Posts: 3426
    
  12
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 ]

Joanne
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
Originally posted by Stevi Deter:
Sometimes, it's better to refactor as

What a good idea!
Pepo moussa
Ranch Hand

Joined: Mar 20, 2008
Posts: 36
so any one can tell me how to make it throw exception ..?
Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: simple try & catch
 
Similar Threads
Adding JLabels to a vector
toString problem
Filling Array
Localized exceptions
exceptions