• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confused about exceptions

 
Tamara Lopez
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A lot of books and tutorials say things along the lines of:
"The Java runtime system requires that a method either catch or specify all checked exceptions that can be thrown by that method"
But I can write classes without throwing or catching anything..... ?
 
Kj Reddy
Ranch Hand
Posts: 1704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you can write. But if your class using any of Java class methods which throws checked exceptions then you need to catch them or throw them back.
 
Emanuel Kadziela
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason you can write classes and methods without worrying about exceptions is that you have not yet encountered an exception that must be handled. Try to compile the following piece of code:



and see what happens...
 
ak pillai
author
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Due to heavy use of checked exceptions and minimal use of unchecked exceptions, there has been a hot debate in the Java community regarding true value of checked exceptions. Use checked exceptions when the client code can take some useful recovery action based on information in exception. Use unchecked exception when client code cannot do anything. For example, convert your SQLException into another checked exception if the client code can recover from it and convert your SQLException into an unchecked (i.e. RuntimeException) exception, if the client code cannot do anything about it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic