Exceptions

Exceptions - Exceptions in Java Creating an Exception Class...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Exceptions in Java Creating an Exception Class A method "throws" an Exception object back to its caller to indicate that it is having a problem. So we need an Exception class to make the Exception object from. This is an easy, usually trivial, task in Java since we are provided an Exception class in the Java class library that we can extend. public class OverdraftException extends Exception { ... If we create the class in Eclipse, on the class declaration panel we can request "constructors from superclass" and we will get constructors in our child Exception class OverdraftException that accept an error message and/or the address of another Exception as constructor parameters. We seldom add any code to an Exception class - we're basically just giving it a meaningful class name! When we create an Exception class, we also get to designate it as a kind of Exception that MUST be caught, or as a kind of Exception that can OPTIONALLY be caught. If we declare the Exception class to extend Exception as shown above, then the compiler will insist that a method that might be thrown the Exception have a catch block for it. If we don't want to insist that our Exception be caught, we extend RuntimeException like this: public class DontHaveToCatchItException extends RuntimeException { ...
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Perhaps surprisingly, about half the Exceptions in Java are of this type! They generally address a situation where, if you're coding properly, you won't get this Exception (like ArrayIndexOutOfBoundsException). The author of the Exception doesn't want to insist you have try/catch structures all over your code that will never be used if you're a contientious programmer! So the compiler won't even tell you about these kinds of Exceptions. But if one is thrown to you at run time, and you don't have a catch block for it, you will be ABENDed. So if we don't have total control over the situation in our code, we may voluntarily catch these anyway. We may find out about them by blowing up in our testing. Or, after a little experience, we may remember some of them.
Background image of page 2
RuntimeException in turn extends Exception , which in turn extends Throwable . Draw a picture (using circles to represent the classes) of an Exception object that has to be caught and one that doesn't. Can you see that they both inherit the full functionality of Exception and Throwable? Can you see how the compiler (and the JVM at run-time) could identify an Exception that had to be caught and one that didn't? Throwing an Exception public void withdraw(int amount) throws IllegalArgumentException, OverdraftException { if (amount < 0) throw new IllegalArgumentException("Withdraw amount is negative"); if (amount > balance) throw new OverdraftException("Amount requested was " + amount + ", balance remains " + balance); balance -= amount; } Here the withdraw() method is testing the parameter amount and may (make and) throw an Exception. When an Exception is thrown, execution also returns immediately
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 8

Exceptions - Exceptions in Java Creating an Exception Class...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online