1. RuntimeExceptions are unchecked exceptions meaning, applications which using the methods neednot know that such and such RuntimeExceptions are being thrown and if they have to know, there are thousands. So by convention, you should not declare RunttimeException in the throws clause
2. Does not matter, if you are creating that exception or its being created by a another method, which you have called, you still have to declare in your throws clause.
Originally posted by Maduranga Liyanage: [ 2) If my method is throwimg an exception, but if I have not expicitly used the "throw" statement, do I still have to declare it in the method signature with "throws" clause?
not necessarily if you are catching in the same code... you only need to put "throws" in the method declaration when you know that where the exception is thrown is not catched but in the other class which called this method ... and you can remove throws class from method declaration when you catch the exception same method code where its is thrown,.
one more thing if again by catch block it is again thrown then method must declare throws clause when that execption is not caught by subsecent catch block...
Originally posted by Maduranga Liyanage: [ 3) If a method has not declared any exceptions with the "throws" clause, why can't I put that code inside a "try/catch" block? [/QB]
you can definetly put the code inside the try/catch block and catch block must be catching all the checked exception..
pls anybody acknolwedge my regarding my reply and correct me if possible...
When you get an EOF exception, it means you were reading a data stream that contained corrupt data. That's not your fault (unless you created the stream ) and there's nothing you can do about it except notify your user. Hopefully the exception's message will be informative. For many checked exceptions, you can't do much more than display the message and back out of the IO operation.
On the other hand, ArithmeticException is only thrown when an integer is divided by zero. What does it really mean to divide, for example, 5 by 0? It doesn't mean anything! So if your code throws ArithmeticException, your algorithm is doing something meaningless. Fix the algorithm!
I meet a lot of people who think runtime exceptions don't need to be checked because they aren't very harmful. In fact, they're extremely harmful. They indicate that code isn't ready to ship. They don't need to be caught because they should never be thrown.
Are you familiar with the serenity prayer? "Grant me serenity to accept the things I cannot change / Courage to change the things I can / And wisdom to know the difference." Checked exceptions are the things you cannot change. Runtime exceptions are the things you can.
Consultant to SCJP team.<br />Co-designer of SCJD exam.<br />Co-author of "Complete Java 2 Certification Study Guide".<br />Author of "Ground-Up Java".
Joined: May 25, 2005
Thanks a lot for your reply. And also I'm glad that I got an answer for my question from an author of the book I am using. Thanks.
Philip I am doing my exam on exactly couple fo days. I've been reading "Sybex Complete Java Certification" and doing the exams and chapter questins from the CD. I need to ask a very important question. As I dont know what the questions in the real exam looks like, can you please tell me how close the questions in the Book's CD exams with the real exam questions? Please, this would help me a lot. If I get around 75% on the exams in the CD, would that be enough to pass the exam??