CMSC 132A Lecture Notes - Lecture 21: Parsing, Exception Handling, Java Virtual Machine

71 views4 pages
CMSC132A Lecture 21: Throwing
Before you can catch an exception, some code somewhere must throw one. Any code can throw an exception:
your code, code from a package written by someone else such as the packages that come with the Java
platform, or the Java runtime environment. Regardless of what throws the exception, it's always thrown with the
throw statement.
As you have probably noticed, the Java platform provides numerous exception classes. All the classes are
descendants of the Throwable class, and all allow programs to differentiate among the various types of
exceptions that can occur during the execution of a program.
You can also create your own exception classes to represent problems that can occur within the classes you
write. In fact, if you are a package developer, you might have to create your own set of exception classes to
allow users to differentiate an error that can occur in your package from errors that occur in the Java platform or
other packages.
You can also create chained
exceptions. For more information, see the Chained Exceptions section.
Throw
All methods use the throw statement to throw an exception. The throw statement requires a single argument:
a throwable object. Throwable objects are instances of any subclass of the Throwable class. Here's an
example of a throw statement.
throw someThrowableObject
;
Let's look at the throw statement in context. The following pop method is taken from a class that implements
a common stack object. The method removes the top element from the stack and returns the object.
public Object pop() {
Object obj;
if (size == 0) {
throw new EmptyStackException();
}
obj = objectAt(size - 1);
setObjectAt(size - 1, null);
Unlock document

This preview shows page 1 of the document.
Unlock all 4 pages and 3 million more documents.

Already have an account? Log in

Document Summary

Before you can catch an exception, some code somewhere must throw one. Any code can throw an exception: your code, code from a package written by someone else such as the packages that come with the java platform, or the java runtime environment. Regardless of what throws the exception, it"s always thrown with the throw statement. As you have probably noticed, the java platform provides numerous exception classes. All the classes are descendants of the throwable class, and all allow programs to differentiate among the various types of exceptions that can occur during the execution of a program. You can also create your own exception classes to represent problems that can occur within the classes you write. For more information, see the chained exceptions section. All methods use the throw statement to throw an exception. The throw statement requires a single argument: a throwable object. Throwable objects are instances of any subclass of the throwable class.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents

Related Questions