Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 28 More on Exceptions COMP1681 / SE15 Introduction to Programming.

Similar presentations

Presentation on theme: "Lecture 28 More on Exceptions COMP1681 / SE15 Introduction to Programming."— Presentation transcript:

1 Lecture 28 More on Exceptions COMP1681 / SE15 Introduction to Programming

2 SE15: More on Exceptions28–2 Today’s Learning Objectives For you to appreciate that exceptions are represented by classes, and that these classes are related For you to understand that these relationships can be exploited when we write exception handlers For you to learn how to throw exceptions from within your own code For you to see how exception classes are written

3 SE15: More on Exceptions28–3 Lecture Outline Recap of basic concepts Exception classes Checked and unchecked exceptions Writing exception handlers Throwing exceptions Writing exception classes

4 SE15: More on Exceptions28–4 Brief Recap Exceptions are created to signal errors in a program Exceptions are objects, containing an error message and possibly other information on the error When an exception is thrown, normal flow of control is suspended Exceptions can be caught by an exception handler, consisting of a try block and catch block If a matching exception handler is not found, an exception will halt the program

5 SE15: More on Exceptions28–5 What Is Printed Here? A.“Hello!” B.“Hello!”, then “1/0 = Infinity” C.“Hello!”, then some error messages D.Nothing at all public static void main(String[] args) { System.out.println("Hello!"); int impossibleResult = 1/0; System.out.println("1/0 = " + impossibleResult); }

6 SE15: More on Exceptions28–6 What Is Printed Here? public static void main(String[] args) { try { System.out.println("Hello!"); int impossibleResult = 1/0; System.out.println("1/0 = " + impossibleResult); } catch (ArithmeticException error) { System.out.println("Doh!"); } }

7 SE15: More on Exceptions28–7 Exception Class Hierarchy Exception classes are related to each other through a mechanism called inheritance Parent class, Exception, defines attributes and behaviour common to all exceptions Each child class represents a specific kind of error

8 SE15: More on Exceptions28–8 Exception Class Hierarchy

9 SE15: More on Exceptions28–9 Exception Class Hierarchy Notice how IOException inherits from Exception, not from RuntimeException…

10 SE15: More on Exceptions28–10 Checked vs. Unchecked An unchecked exception is an instance of a class inheriting ultimately from RuntimeException For programming errors, e.g. precondition violations A checked exception inherits from Exception but not from RuntimeException For conditions from which caller may be able to recover, e.g. file not found

11 SE15: More on Exceptions28–11 Checked vs. Unchecked public void openFile(String filename) throws FileNotFoundException { input = new FileInputStream(filename); } public int divideBy(int a, int b) { return a/b; } Method that can throw a checked exception Method that can throw an unchecked exception Exception specification

12 SE15: More on Exceptions28–12 Multiple catch Blocks & finally Block Each try block can have multiple catch blocks Blocks are processed top-to-bottom First matching block is executed, others are ignored Each try block can have a finally block Always executes, regardless of whether there was an exception or not

13 SE15: More on Exceptions28–13 Throwing Exceptions Exceptions can be generated with a throw statement Instances of standard exception classes should be thrown if possible Exceptions are objects, so we create them with new A String argument, representing an error message, can be passed in when creating an exception object

14 SE15: More on Exceptions28–14 Example class Date { private int day, month, year;... public void setMonth(int theMonth) { if (theMonth 12) throw new IllegalArgumentException("Bad month!"); month = theMonth; } } Compare with the example from last lecture…

15 SE15: More on Exceptions28–15 Creating Your Own Exception Types 1.Decide on a name for your exception class 2.Decide whether it should be checked or unchecked Former more likely; the need for unchecked exceptions is usually satisfied by standard exception classes 3.Inherit from Exception or RuntimeException, as appropriate 4.Specify instance variables to hold other data (possibly) 5.Write a constructor with a String parameter

16 SE15: More on Exceptions28–16 Example public class MyException extends Exception { public MyException(String message) { super(message); }... } Do this only if standard exception classes won’t suffice!

17 SE15: More on Exceptions28–17 Golden Rules Use checked exceptions for recoverable conditions, unchecked for programming errors Avoid unnecessary use of checked exceptions Favour use of standard exception classes Don’t catch exceptions in contexts where you can’t handle them; let them be handled elsewhere Never throw an exception and catch it in same place Never swallow exceptions silently

18 SE15: More on Exceptions28–18 Follow-up Work Savitch, Chapter 8 This week’s exercise

Download ppt "Lecture 28 More on Exceptions COMP1681 / SE15 Introduction to Programming."

Similar presentations

Ads by Google