File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exceptions Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exceptions Doubt " Watch "Exceptions Doubt " New topic
Author

Exceptions Doubt

Phungsuk Wangdu
Ranch Hand

Joined: Nov 04, 2009
Posts: 114
well i have doubt about exceptions
first of all can we throw runtime exceptions by using throw keyword
and if yes than do we have to declare it in method signature

second if we are not having any code in try block and if we do implement catch blocks do we get comile error or runtime exception

and could you provide a link to get information about all checked and unchecked exceptions
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Hey,

yes you can throw RuntimeExceptions and yes it is unchecked. You do that when you don't want the client to handle the exception, for instance when it is not likely to recover from it. Check this piece of code.


IllegalArgumentException is a runtime exception, and in this case it really does'nt make any sense to have the client handle the event.

If you have a try catch block and nothing in the try, the compiler will detect that this exception cannot be thrown and complain ;-).


JDBCSupport - An easy to use, light-weight JDBC framework -
Phungsuk Wangdu
Ranch Hand

Joined: Nov 04, 2009
Posts: 114
well that means if we are throwing runtime exceptions then there is no need to declare it in method signature
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
the compiler only complain when you have declared that a checked exception is going to be thrown and is not thrown....actually the checked exception should also have no unchecked exceptions as subclasses...for example ioexception....throw new EXception() wouldnt be a problem has subclass of exception clas includes runtime exceptions which are unchecked excptions


scjp 1.6 91%, preparing for scmad
"Time to get MAD now.. we will get even later"....by someone unknown
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Mahesh Chitale wrote:well that means if we are throwing runtime exceptions then there is no need to declare it in method signature


No you don't have to, but you usually want to for the sake of completeness.

The method above should look like this



Again, you don't have to, but you do. To make your API clearer to people who use it.
Muhammad Khojaye
Ranch Hand

Joined: Apr 12, 2009
Posts: 449

Mahesh Chitale wrote:well that means if we are throwing runtime exceptions then there is no need to declare it in method signature

Well, if the exception thrown is not a checked exception, the declaration of the method do not require to state that.
could you provide a link to get information about all checked and unchecked exceptions

see this and this. Hope it helps.


http://muhammadkhojaye.blogspot.com/
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Raju Champaklal wrote:the compiler only complain when you have declared that a checked exception is going to be thrown and is not thrown....

No the compiler does not complain if you declare that a checked exception will be thrown but is not thrown. It however does complain when you try to catch a checked exception with code that does not throw that checked exception.

Does not complain:


Does complain:


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
going to be thrown meant by being caught in the catch block and might be thrown in the throws clause ....i interpreted it this way...maybe the way i wrote it was wrong
Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 255

If you have a try catch block and nothing in the try, the compiler will detect that this exception cannot be thrown and complain ;-).


The compiler give error only if we try to catch checked exception in catch and we do not have anything in try.. for unchecked exception it does not give any error./
Ex.. the following code works fine..



Regards
Salil Verma
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exceptions Doubt