lecture22

lecture22 - PIC 20A Lecture 22: Exception Handling What is...

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

View Full Document Right Arrow Icon
PIC 20A Lecture 22: Exception Handling
Background image of page 1

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

View Full DocumentRight Arrow Icon
What is an exception? • an indication that a problem occurred during the program's execution • modeled in Java by Throwable and Exception objects
Background image of page 2
Examples of exceptions • out-of-bounds array subscript • arithmetic overflow • division by zero • invalid method parameters • trying to use a null reference • memory exhaustion
Background image of page 3

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

View Full DocumentRight Arrow Icon
Example • suppose I have the method public void setAge(int age){ _age = age; } Question: What do I do if the user enters an age less than 0? Answer: throw an exception
Background image of page 4
Throwing an Exception public void setAge(int age) { if(age < 0){ String msg = "age cannot be negative"; throw new IllegalArgumentException(msg); } _age = age; } Notice we do not handle the exception here. How can we? The caller of this method is to blame! It should handle it!
Background image of page 5

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

View Full DocumentRight Arrow Icon
What did we do? We created an IllegalArgumentException and threw it in method setAge. Control goes from the throw point to the caller of this method The caller must handle the exception or pass it up to its caller and so forth. ..
Background image of page 6
What can I throw? • You can throw any object of class Throwable • Throwable has subclasses Error and Exception has a printStackTrace method that prints a stack trace ( method call stack ) for the object. • Exception has subclasses such as RuntimeException and IOException
Background image of page 7

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

View Full DocumentRight Arrow Icon
Being more specific is good • We can and should define our own subclasses of Exception and RuntimeException public class InvalidAgeException extends IllegalArgumentException { }
Background image of page 8
Examples of RuntimeException s • ArithmeticException • ClassCastException • IllegalArgumentException • IndexOutOfBoundsException • NullPointerException
Background image of page 9

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

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

Page1 / 24

lecture22 - PIC 20A Lecture 22: Exception Handling What is...

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

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