Lecture 19 - Exception Handling

Lecture 19 - Exception Handling - Exception Handling Java...

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

View Full Document Right Arrow Icon
Exception Handling Java provides a structured means to handle errors using try-catch blocks o Less cluttered than the if-else blocks typically used in other languages o Error handling code is separated from “regular code” An exception is a runtime error o Eg: z = x/y; //Gives an error when y is 0 When the error is encountered, an exception object is thrown The exception is caught (or handled) by one of the following (each is tried in order) o A surrounding try-catch block o A try-catch block in calling code o The JVM Library Exception Classes Java provides many of these Find in documentation Custom Exception Classes Are normally created by extending Exception o Eg: public class InedibleException extends Exception { public InedibleException() { super(“Object can’t be counted.”); } } You can create specific subclasses for each kind of error in your program Throwing Exceptions When a method detects an error, it “throws” a newly-created exception object o Control immediately “jumps” to the calling code
Background image of page 1

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

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

Page1 / 4

Lecture 19 - Exception Handling - Exception Handling Java...

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

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