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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Throws Clause" Watch "Throws Clause" New topic
Author

Throws Clause

michael delta
Ranch Hand

Joined: Aug 11, 2009
Posts: 35

So, I've used the throws clause several times, but only because of the compiler's warnings. I know that an exception must either be caught(try,catch) or declared(throws), but i'm not sure i fully understand this strategy.
Let me do a short recap and please tell me if i'm wrong:

1. Throws clause tells the compiler and the client that the a method could throw an exception.
2. Is used in a method which calls another method that throws an uncaught exception.

Any information would be more than helpful!
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

michael delta wrote:
2. Is used in a method which calls another method that throws an uncaught exception.


Used in the method header/declaration.


Mohamed Sanaulla | My Blog
michael delta
Ranch Hand

Joined: Aug 11, 2009
Posts: 35

Used in the method header/declaration.

Yep, that's what i meant to say, didn't express it correctly.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4168
    
  21

The strategy here is to pass the Exception along the method call stack until some code knows how to recover of handle the Exception. For example, a JDBC library may cause a SQLException to occur. That library can't know what you need to do to fix the exception, so it throws it. Your immediate method which calls the JDBC library may not know what to do either, maybe it is too far down the chain to be able to do anything to fix the problem, so your immediate calling function throws the exception as well. So you keep passing the function up the method calls until finally a method can do something with it. Perhaps it can check the SQLException and determine that it needs to clean up an old connection, reset some variables, open a new connection, and re-try the entire operation from scratch. Maybe it can figure out the exception comes from bad data, and so can log it, inform the GUI, and skip the operation. Maybe the exception signals the certain death of all future operations, and so it logs it, cleans up resources, and exits the application gracefully.

So the point is, the immediate code which generates the exception may not know what to do, so you need to keep throwing it until you get to code that can respond appropriately and safely. Once you are at a level which can respond you need to catch the Exception and take appropriate action.


Steve
michael delta
Ranch Hand

Joined: Aug 11, 2009
Posts: 35

Excellent answer, thank you!
antty ge
Greenhorn

Joined: Dec 24, 2010
Posts: 7
if the code may gose wrong what you don't know the reason ,you should try catch around it ,and if you add a declare of throws before a method ,it's means that the method may throw an exception and you should try catch is or throw the exception to the father method.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
Welcome to the Ranch

Has nobody referred to the Java™ Tutorials section? You will find an explanation there.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Throws Clause
 
Similar Threads
Catching non-thrown exceptions
stateful session bean instance removal
Which Exceptions a method can throw?
Exceptions
OCP JP Practice Exam 2 #29