1 / 9

Exceptions

Learn about exceptions in programming, from dividing by 0 to handling file not found errors. Understand call stack traces and exception propagation, and explore the exception class hierarchy. Discover how to design and throw your own exceptions effectively. Put your knowledge to the test with a configuration file processing program.

edwinab
Download Presentation

Exceptions

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. Exceptions

  2. Overview • “An exception is an object that defines an unusual or erroneous situation.” • Examples • Divide by 0 • Array index out of bounds • File cannot be found • Follow a null reference

  3. Uncaught Exceptions int a = 5; int b = 0; int c = a/b; Exception in thread "main" java.lang.ArithmeticException: / by zero at ExceptionsExamples.main(ExceptionsExamples.java:12)

  4. Uncaught Exceptions • Call stack trace • info about where the exception occurred • info about all methods that were called to get to the method where the exception occurred Exception in thread "main" java.lang.ArithmeticException: / by zero at ExceptionsExamples.caller2(ExceptionsExamples.java:20) at ExceptionsExamples.caller1(ExceptionsExamples.java:12) at ExceptionsExamples.main(ExceptionsExamples.java:8)

  5. try/catch try { //statements that may throw an exception } catch(Exception_Type name) { //what to do in case of exception //Exception_Type } catch(Another_Type another_name) { //what to do in case of exception Another_Type }

  6. Propagation void divider() throws ArithmeticException { int a = 5; int b = 0; int c = a/b; }

  7. Exception Class Hierarchy • Throwable • Error • Exception • IOException • EOFException, FileNotFoundException • ClassNotFoundException • RuntimeException • ArithmeticException • ClassCastException • NullPointerException

  8. Designing/Throwing Own Exception • Create a class that extends Exception • OutOfRangeException if out of range { throw new OutOfRangeException(“message”); }

  9. Exercise • Write a program to process a configuration file.

More Related