Different Ways of Handling Exceptions

     

Let's look at a short demo class that allows us to see everything in one place. This example shows different ways of working with code that could generate exceptions. You might not be familiar with working with Java's networking libraries or database libraries. It doesn't matter here. What matters is noticing the different ways that exceptions can be used in your code.

 

 package net.javagarage.demo.exceptions; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.sql.*; /**  * This class demonstrates different ways to write  * your methods when you need to deal with exceptions  * in them. Any one of them could be most appropriate,  * depending on the situation.  *  * @author eben hewitt  */ public class DemoExceptionDeclarations {   //ONE THROWS. this method does an network   //operation, but doesn't want to deal with it.   //so pass the buck... public static InputStream getDocumentAsInputStream(URL url) throws IOException { InputStream in = url.openStream(); return in;    }   //NO THROWS, MULTIPLE CATCH. doesn't throw   //anything. it deals with the exception itself.   //(This method must be the first born.)   private void myNetMethod() {   URL url = null;   try { //if this throws an exception, we jump down to //MalformedURLException   url = new URL("http://javagarage.net:80/index.html");   //if this throws an exception, say, because of   //network problems, or because there is no resource   //at the URL, then processing jumps to the IOException   url.openStream();   } catch (MalformedURLException mue) {   //getMessage() tells you details about what happened System.err.println("MalformedURLException: " + mue.getMessage()); //prints the stack trace to the standard error stream mue.printStackTrace();   } catch (IOException ioe) {   System.err.println("Could not open stream to              URL " + url);   }   }   //CATCH NESTED IN FINALLY. this method does   //operations that make her catch   //multiple exceptions. that's okay, just add   //multiple catch blocks. Make sure that your catch   //blocks go from narrowest to widest public void databaseSelect(int idIn) { ResultSet rs = null; Connection connection = null;//get connection try { PreparedStatement getStuff; String s = "SELECT itemName, price FROM Products             WHERE id = ?"; getStuff = connection.prepareStatement(s); getStuff.setInt(1, idIn); rs = getStuff.executeQuery(); //do something with result set here. //if the above code generates an exception of type //SQLException, this catch block runs } catch (SQLException sqle){ System.err.println("Could not execute query:\n" + sqle.getMessage()); //NESTED TRY/CATCH try { rs.close(); } catch(SQLException se){ System.err.println("Could not close result set.\n" + se.getCause()); } //if the code above in the first try block of the //method generates any other type of exception, //this will catch it, since Exception is the parent } catch (Exception e){ e.printStackTrace(); //whether or not any exceptions were thrown above, //this code will execute no matter what } finally { try { //clean up after ourselvesbut this also throws //SQLException, so we will just deal with it here. connection.close(); } catch(SQLException se){ System.err.println("Could not close connection to DB.\n" + se.getCause()); } } } } 

Each method in the preceding class shows a different way of dealing with exceptions, and should serve as a reasonable guide as you write your own exception handling code.



Java Garage
Java Garage
ISBN: 0321246233
EAN: 2147483647
Year: 2006
Pages: 228
Authors: Eben Hewitt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net