Throwing Exceptions

     

Your method might not be the very best place to handle an exception. It might make more sense to let the exception pass through your method unhandled, and make some other guy deal with it. You can do that. But you have to say that that is what you're doing. You do so with the throws keyword. The following modified method will also compile and run:

 

 public void someMethod ()  throws IOException  {        File f = new File("s");        f.getCanonicalFile(); } 

Do not declare that your method throws unchecked exceptions that inherit from Runtime-Exception. Do not write the following:

 

 void do(int i)  throws NullPointerException  //BAD DOG! NO! {      //blah blah blah } 

Why should you never do that? Sometimes the truth is tough. If you are that worried about your code generating a NullPointerException, you probably have an idea of where the problem is, and you should do something to deal with it at that point. Remember that because unchecked exceptions mean that you are trusted, don't advertise to the world with a great big throws sign, "Hey! Look at me! I wrote some crappy code! I might divide integers by zero!" It's just unseemly. Alternatively, sometimes unchecked exceptions occur because of things that are totally out of your control. I say, "If there's nothing you can do about it, there's no point in talking about it."

One other note: You can throw multiple exceptions by separating them with a comma when you declare them. Like this:

 

 public void myMethod() throws FileNotFoundException,        EOFException 

Of course, it is legal to write something like the following:

 

 private void myMethod() throws IOException, FileNotFoundException...//Don't do this! 

But don't do that. Because it is redundant. IOException is bigger (higher up the inheritance hierarchy than FileNotFoundException ), and you might as well just deal with what you should deal with, and stay on track.

You can also throw an exception using the new keyword if you want to reroute the flow of your program. You do so like this:

 

 public void calculateBillTotal(float bill,                                float tax,                                float tip)                throws CheapskateException { if (tip < bill * .15)       throw new CheapskateException(             "Fork it over, cheapskate"); return bill + tax + tip; } 

In the preceding code, if the tip is less than 15% of the bill, the exception is thrown up to the caller. The return statement that adds the parts of the bill together will never be executed.

Note one other thing. Sometimes you have to say stuff like this, just to make sure. There's a nut job in every crowd : you can't throw anything that ain't throwable . That is, don't try the following, smart guy:

 

 public static int myMethod throws String //you know better 

How would you like it if people came to your house and threw a JFrame ? Somebody could get hurt on one of those things.

"Is plastic okay? Maybe. Maybe nobody can say for sure "

J. Benton, 1.16.04



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