aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exceptions 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 "Exceptions " Watch "Exceptions " New topic
Author

Exceptions

Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
Hi.. I have few questions about throwing exceptions:

1) If I'm "throwing" a subclass of RuntimeException or Error classes in a method, SHOULD I or MUST I declare it in the method signature with a "throws" clause?

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?

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?

My Borland JBuilder is giving different results at different times, so I can't get a concrete answer for these questions.. Please help guys..
Thanks..
Santhosh Kumar
Ranch Hand

Joined: Nov 07, 2000
Posts: 242
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.

3. You can put it actually.



Im using java version "1.4.2_08"
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
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?

[/QB]


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...


Thanks and Regards, Amit Taneja
Philip Heller
author
Ranch Hand

Joined: Oct 24, 2000
Posts: 119
The big issue here is that some kinds of problems are completely avoidable through competent programming, while others can't be avoided no matter what.

Extreme examples: java.io.StreamCorruptedException versus java.lang.ArithmeticException.

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".
Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
Hi Philip..

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??

Thanks a lot guys.. :-)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exceptions