1 / 9

Java Exception Handling Tutorial

Learn about exceptions in Java, including creating, throwing, and catching exceptions. Practice by defining and throwing exceptions using conditional structures.

lsabo
Download Presentation

Java Exception Handling Tutorial

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 112 - Fall 2012, Lab 07 Haohan Zhu

  2. CS 112 - Fall 2012, Lab 07 Exception • Tutorial • http://docs.oracle.com/javase/tutorial/essential/exceptions/

  3. CS 112 - Fall 2012, Lab 07 Exception • Class Throwable • http://docs.oracle.com/javase/7/docs/api/java/lang/Throwable.html • Class Exception • http://docs.oracle.com/javase/7/docs/api/java/lang/Exception.html • Class Error • http://docs.oracle.com/javase/7/docs/api/java/lang/Error.html

  4. CS 112 - Fall 2012, Lab 07 Exception • Definition: • An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions.

  5. CS 112 - Fall 2012, Lab 07 Exception • Create an Exception • There exists many exceptions in Java • Throw an Exception • Throws clause needed for checked exceptions • Catch an Exception • Try-Catch-Finally Blocks

  6. CS 112 - Fall 2012, Lab 07 Create an Exception • Extends Throwable or Exception • public class BadInputsException extends Throwable • { • public BadInputsException(String msg) • { • super(msg); • } • }

  7. CS 112 - Fall 2012, Lab 07 Throw an Exception • Throws clause for checked exception (not Error or RuntimeException) • public class ThrowException { • public static void main(String[] args) throws BadInputsException{ • throw new BadInputsException("Hello World"); • } • }

  8. CS 112 - Fall 2012, Lab 07 Catch an Exception • Try-Catch-Finally Blocks • try { • throw new BadInputsException("msg"); • } • catch (BadInputsException e) { • System.err.println("Catch a BadInputsException"); • } • finally { • System.out.println("Finished"); • }

  9. CS 112 - Fall 2012, Lab 07 Practice • Define 2 exceptions • Throw exceptions by using a conditional structure (if - elseif - else)

More Related