File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exceptions Doubt

 
Phungsuk Wangdu
Ranch Hand
Posts: 114
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1183
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ;-).
 
Phungsuk Wangdu
Ranch Hand
Posts: 114
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well that means if we are throwing runtime exceptions then there is no need to declare it in method signature
 
Raju Champaklal
Ranch Hand
Posts: 521
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 449
IntelliJ IDE Java Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Raju Champaklal
Ranch Hand
Posts: 521
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 255
Hibernate Oracle Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic