Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Beginning Java and the fly likes Confused about exceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Confused about exceptions" Watch "Confused about exceptions" New topic

Confused about exceptions

Tamara Lopez
Ranch Hand

Joined: Feb 21, 2006
Posts: 63
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

Joined: Sep 20, 2003
Posts: 1704
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

Joined: Mar 24, 2005
Posts: 187
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
Ranch Hand

Joined: Feb 11, 2006
Posts: 288
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.

java j2ee job interview questions with answers | Learn the core concepts and the key areas
I agree. Here's the link:
subject: Confused about exceptions
It's not a secret anymore!