It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception" Watch "Exception" New topic


luk Hann
Ranch Hand

Joined: Jan 29, 2001
Posts: 48
Could anybody give me a clear shot when I should expect to use "try-catch" to handle exception, when "throws" and when both? Thanks.
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
You should use try/catch anytime you are about to execute some code that might run into trouble. For instance if you are about to divide by an integer variable there is always the chance that the variable will hold a zero and cause and arithmeticException. And IO causes all sorts of problems and you should deal with it so your program does not crash. The classes that might cause trouble Implement or extend some sort of Exceptions.
Once you catch an Exception it is up to you whether to deal with it (print out a cute message, ask the user for better input, etc.) or throw it to a super class to deal with (like when you have not a clue how to deal with the problem or are just to lazy).
[This message has been edited by Cindy Glass (edited February 02, 2001).]

"JavaRanch, where the deer and the Certified play" - David O'Meara
I agree. Here's the link:
subject: Exception
jQuery in Action, 3rd edition