File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Exception Handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exception Handling" Watch "Exception Handling" New topic

Exception Handling

David Dickinson
Ranch Hand

Joined: Nov 11, 2004
Posts: 66
Hi guys,

I've been tidying up my code and creating the relevant javadoc comments and i've realised I haven't wrote a great deal of exception handling.

Its always been an area which has confused me slightly and i'm looking for a little advice on what is standard practice.

As far as I can see the only real errors which will occur in my program will be the file i'm reading from not existing and possibly mis placed values in a file so I might try and create a double from a string etc.

These all seem to be IOExceptions and all through my code I have thrown IOExceptions from methods etc.

Now I need to catch them is there a simple way to put a single catch statement in the "topmost" calling class which simply closes the program and outputs a single line to the console highlighting the error?

Thanks guys!
Barry Higgins
Ranch Hand

Joined: Jun 05, 2003
Posts: 89
To pass the IOException or any exception "upwards" decalre your function as throwing this exception and take out your try/catch blocks

eg. public void openFile throws IOException {}

This will tell the function that calls openFile that this funtion throws an IOExceotion which needs to be handled leaving the option to wrap this call with a try/catch or pass it "upwards" in the same way. You can keep throwing up untill you reach your upmost function and wrap this with a try catch.

With regard to using exceptions elsewhere in your code you may consider using them yourself where unexpected events occurs. You don't just have to catch the exceptions that the SDK functions throw to you!

Hope this helps,
I agree. Here's the link:
subject: Exception Handling
It's not a secret anymore!