A Third Look At Java. Chapter Seventeen Modern Programming Languages, 2nd ed. 1

Similar documents
Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

More on Exception Handling

C16b: Exception Handling

BBM 102 Introduction to Programming II Spring Exceptions

More on Exception Handling

Exceptions. References. Exceptions. Exceptional Conditions. CSE 413, Autumn 2005 Programming Languages

Exceptions. CSE 142, Summer 2002 Computer Programming 1.

Exceptions. Readings and References. Exceptions. Exceptional Conditions. Reading. CSE 142, Summer 2002 Computer Programming 1.

Topic 6: Exceptions. Exceptions are a Java mechanism for dealing with errors & unusual situations

Introduction to Software Design

Data Structures. 02 Exception Handling

ITI Introduction to Computing II

Program Correctness and Efficiency. Chapter 2

BBM 102 Introduction to Programming II Spring 2017

ITI Introduction to Computing II

Exceptions. CSC207 Winter 2017

CSC207H: Software Design. Exceptions. CSC207 Winter 2018

Exceptions - Example. Exceptions - Example

Chapter 9. Exception Handling. Copyright 2016 Pearson Inc. All rights reserved.

About This Lecture. Outline. Handling Unusual Situations. Reacting to errors. Exceptions

Introduction to Java. Handout-3a. cs402 - Spring

COMP1008 Exceptions. Runtime Error

EXCEPTION HANDLING. Summer 2018

CS 3 Introduction to Software Engineering. 3: Exceptions

Programming II (CS300)


National University. Faculty of Computer Since and Technology Object Oriented Programming

What is the purpose of exceptions and exception handling? Vocabulary: throw/raise and catch/handle Exception propagation Java checked and unchecked

CSCI 261 Computer Science II

Exceptions in Java

CSC System Development with Java. Exception Handling. Department of Statistics and Computer Science. Budditha Hettige

Exception Handling. Sometimes when the computer tries to execute a statement something goes wrong:

Fundamentals of Object Oriented Programming

Exception Handling. Run-time Errors. Methods Failure. Sometimes when the computer tries to execute a statement something goes wrong:

Exceptions. What exceptional things might our programs run in to?

Internal Classes and Exceptions

COE318 Lecture Notes Week 10 (Nov 7, 2011)

COMP 213. Advanced Object-oriented Programming. Lecture 17. Exceptions

Le L c e t c ur u e e 5 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Exception Handling

Correctness and Robustness

Programming II (CS300)

Object Oriented Programming

Types, Values and Variables (Chapter 4, JLS)

Object Oriented Programming Exception Handling

EXCEPTIONS. Objectives. The try and catch Statements. Define exceptions. Use try, catch and finally statements. Describe exception categories

Day 8. COMP1006/1406 Summer M. Jason Hinek Carleton University

EXCEPTION HANDLING. // code that may throw an exception } catch (ExceptionType parametername) {

Exception Handling Introduction. Error-Prevention Tip 13.1 OBJECTIVES

Exceptions. Errors and Exceptions. Dealing with exceptions. What to do about errors and exceptions

EXCEPTIONS. Java Programming

Assertions and Exceptions Lecture 11 Fall 2005

Pages and 68 in [JN] conventions for using exceptions in Java. Chapter 8 in [EJ] guidelines for more effective use of exceptions.

Comp 249 Programming Methodology Chapter 9 Exception Handling

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

Typecasts and Dynamic Dispatch. Dynamic dispatch

Programming Languages and Techniques (CIS120)

EXCEPTION-HANDLING INTRIVIEW QUESTIONS

Errors and Exceptions

Java Primer. CITS2200 Data Structures and Algorithms. Topic 2

Object Oriented Programming

Sri Vidya College of Engineering & Technology Question Bank

Inheritance. SOTE notebook. November 06, n Unidirectional association. Inheritance ("extends") Use relationship

CS1020 Data Structures and Algorithms I Lecture Note #8. Exceptions Handling exceptional events

09/08/2017 CS2530 INTERMEDIATE COMPUTING 9/8/2017 FALL 2017 MICHAEL J. HOLMES UNIVERSITY OF NORTHERN IOWA TODAY S TOPIC: Exceptions and enumerations.

For more details on SUN Certifications, visit

Exception-Handling Overview

COMP200 EXCEPTIONS. OOP using Java, based on slides by Shayan Javed

Lecture 19 Programming Exceptions CSE11 Fall 2013

Exceptions and I/O: sections Introductory Programming. Errors in programs. Exceptions

Introductory Programming Exceptions and I/O: sections

Pace University. Fundamental Concepts of CS121 1

17. Handling Runtime Problems

Exceptions vs. Errors Exceptions vs. RuntimeExceptions try...catch...finally throw and throws

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

Exceptions. Examples of code which shows the syntax and all that

ECE 122. Engineering Problem Solving with Java

Chapter 8. Exception Handling. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

COMP 401 EXCEPTIONS. Instructor: Prasun Dewan

Exceptions Chapter 10. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

CSC 1214: Object-Oriented Programming

6.Introducing Classes 9. Exceptions

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Exceptions Handling Errors using Exceptions

CS 61B Data Structures and Programming Methodology. July 7, 2008 David Sun

Recitation 3. 2D Arrays, Exceptions

Introduction to Programming Using Java (98-388)

Administration. Exceptions. Leftovers. Agenda. When Things Go Wrong. Handling Errors. CS 99 Summer 2000 Michael Clarkson Lecture 11

Full file at Chapter 2 - Inheritance and Exception Handling

Exceptions and Libraries

COMP-202. Exceptions. COMP Exceptions, 2011 Jörg Kienzle and others

CS193j, Stanford Handout #25. Exceptions

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Introduction. Exceptions: An OO Way for Handling Errors. Common Runtime Errors. Error Handling. Without Error Handling Example 1

Stacks and Queues. Gregory D. Weber. CSCI C243 Data Structures

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

Prelim 1 SOLUTION. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants. Recursion OO Short answer

Program Fundamentals

Transcription:

A Third Look At Java Chapter Seventeen Modern Programming Languages, 2nd ed. 1

A Little Demo public class Test { public static void main(string[] args) { int i = Integer.parseInt(args[0]); int j = Integer.parseInt(args[1]); System.out.println(i/j); > javac Test.java > java Test 6 3 2 > Chapter Seventeen Modern Programming Languages, 2nd ed. 2

Exceptions > java Test Exception in thread "main" java.lang.arrayindexoutofboundsexception: 0 at Test.main(Test.java:3) > java Test 6 0 Exception in thread "main" java.lang.arithmeticexception: / by zero at Test.main(Test.java:4) In early languages, that s all that happened: error message, core dump, terminate. Modern languages like Java support exception handling. Chapter Seventeen Modern Programming Languages, 2nd ed. 3

Outline 17.2 Throwable classes 17.3 Catching exceptions 17.4 Throwing exceptions 17.5 Checked exceptions 17.6 Error handling 17.7 Finally 17.8 Farewell to Java Chapter Seventeen Modern Programming Languages, 2nd ed. 4

Some Predefined Exceptions Java Exception Code to Cause It NullPointerException String s = null; s.length(); ArithmeticException int a = 3; int b = 0; int q = a/b; ArrayIndexOutOfBoundsException int[] a = new int[10]; a[10]; ClassCastException Object x = new Integer(1); String s = (String) x; StringIndexOutOfBoundsException String s = "Hello"; s.charat(5); Chapter Seventeen Modern Programming Languages, 2nd ed. 5

An Exception Is An Object The names of exceptions are class names, like NullPointerException Exceptions are objects of those classes In the previous examples, the Java language system automatically creates an object of an exception class and throws it If the program does not catch it, it terminates with an error message Chapter Seventeen Modern Programming Languages, 2nd ed. 6

Throwable Classes To be thrown as an exception, an object must be of a class that inherits from the predefined class Throwable There are four important predefined classes in that part of the class hierarchy: Throwable Error Exception RuntimeException Chapter Seventeen Modern Programming Languages, 2nd ed. 7

Classes derived from Error are used for serious, systemgenerated errors, like OutOfMemoryError, that usually cannot be recovered from Error Object Throwable Exception Java will only throw objects of a class descended from Throwable RuntimeException Classes derived from RuntimeException are used for ordinary systemgenerated errors, like ArithmeticException Classes derived from Exception are used for ordinary errors that a program might want to catch and recover from Chapter Seventeen Modern Programming Languages, 2nd ed. 8

Outline 17.2 Throwable classes 17.3 Catching exceptions 17.4 Throwing exceptions 17.5 Checked exceptions 17.6 Error handling 17.7 Finally 17.8 Farewell to Java Chapter Seventeen Modern Programming Languages, 2nd ed. 9

The try Statement <try-statement> ::= <try-part> <catch-part> <try-part> ::= try <compound-statement> <catch-part> ::= catch (<type> <variable-name>) <compound-statement> Simplified full syntax later The <type> is a throwable class name Does the try part Does the catch part only if the try part throws an exception of the given <type> Chapter Seventeen Modern Programming Languages, 2nd ed. 10

Example public class Test { public static void main(string[] args) { try { int i = Integer.parseInt(args[0]); int j = Integer.parseInt(args[1]); System.out.println(i/j); catch (ArithmeticException a) { System.out.println("You're dividing by zero!"); This will catch and handle any ArithmeticException. Other exceptions will still get the language system s default behavior. Chapter Seventeen Modern Programming Languages, 2nd ed. 11

Example > java Test 6 3 2 > java Test 6 0 You're dividing by zero! > java Test Exception in thread "main" java.lang.arrayindexoutofboundsexception: 0 at Test.main(Test.java:3) Catch type chooses exceptions to catch: ArithmeticException got zero division RuntimeException would get both examples above Throwable would get all possible exceptions Chapter Seventeen Modern Programming Languages, 2nd ed. 12

After The try Statement A try statement can be just another in a sequence of statements If no exception occurs in the try part, the catch part is not executed If no exception occurs in the try part, or if there is an exception which is caught in the catch part, execution continues with the statement following the try statement Chapter Seventeen Modern Programming Languages, 2nd ed. 13

Exception Handled System.out.print("1, "); try { String s = null; s.length(); catch (NullPointerException e) { System.out.print("2, "); System.out.println("3"); This just prints the line 1, 2, 3 Chapter Seventeen Modern Programming Languages, 2nd ed. 14

Throw From Called Method The try statement gets a chance to catch exceptions thrown while the try part runs That includes exceptions thrown by methods called from the try part Chapter Seventeen Modern Programming Languages, 2nd ed. 15

Example void f() { try { g(); catch (ArithmeticException a) { If g throws an ArithmeticException, that it does not catch, f will get it In general, the throw and the catch can be separated by any number of method invocations Chapter Seventeen Modern Programming Languages, 2nd ed. 16

If z throws an exception it does not catch, z s activation stops then y gets a chance to catch it; if it doesn t, y s activation stops and so on all the way back to f z s activation record y s activation record... g s activation record f s activation record Chapter Seventeen Modern Programming Languages, 2nd ed. 17

Long-Distance Throws That kind of long-distance throw is one of the big advantages of exception handling All intermediate activations between the throw and the catch are stopped and popped If not throwing or catching, they need not know anything about it Chapter Seventeen Modern Programming Languages, 2nd ed. 18

Multiple catch Parts <try-statement> ::= <try-part> <catch-parts> <try-part> ::= try <compound-statement> <catch-parts> ::= <catch-part> <catch-parts> <catch-part> <catch-part> ::= catch (<type> <variable-name>) <compound-statement> To catch more than one kind of exception, a catch part can specify some general superclass like RuntimeException But usually, to handle different kinds of exceptions differently, you use multiple catch parts Chapter Seventeen Modern Programming Languages, 2nd ed. 19

Example public static void main(string[] args) { try { int i = Integer.parseInt(args[0]); int j = Integer.parseInt(args[1]); System.out.println(i/j); catch (ArithmeticException a) { System.out.println("You're dividing by zero!"); catch (ArrayIndexOutOfBoundsException a) { System.out.println("Requires two parameters."); This will catch and handle both ArithmeticException and ArrayIndexOutOfBoundsException Chapter Seventeen Modern Programming Languages, 2nd ed. 20

Example public static void main(string[] args) { try { int i = Integer.parseInt(args[0]); int j = Integer.parseInt(args[1]); System.out.println(i/j); catch (ArithmeticException a) { System.out.println("You're dividing by zero!"); catch (ArrayIndexOutOfBoundsException a) { System.out.println("Requires two parameters."); catch (RuntimeException a) { System.out.println("Runtime exception."); Chapter Seventeen Modern Programming Languages, 2nd ed. 21

Overlapping Catch Parts If an exception from the try part matches more than one of the catch parts, only the first matching catch part is executed A common pattern: catch parts for specific cases first, and a more general one at the end Note that Java does not allow unreachable catch parts, or unreachable code in general Chapter Seventeen Modern Programming Languages, 2nd ed. 22

Outline 17.2 Throwable classes 17.3 Catching exceptions 17.4 Throwing exceptions 17.5 Checked exceptions 17.6 Error handling 17.7 Finally 17.8 Farewell to Java Chapter Seventeen Modern Programming Languages, 2nd ed. 23

The throw Statement <throw-statement> ::= throw <expression> ; Most exceptions are thrown automatically by the language system Sometimes you want to throw your own The <expression> is a reference to a throwable object usually, a new one: throw new NullPointerException(); Chapter Seventeen Modern Programming Languages, 2nd ed. 24

Custom Throwable Classes public class OutOfGas extends Exception { System.out.print("1, "); try { throw new OutOfGas(); catch (OutOfGas e) { System.out.print("2, "); System.out.println("3"); Chapter Seventeen Modern Programming Languages, 2nd ed. 25

Using The Exception Object The exception that was thrown is available in the catch block as that parameter It can be used to communicate information from the thrower to the catcher All classes derived from Throwable inherit a method printstacktrace They also inherit a String field with a detailed error message, and a getmessage method to access it Chapter Seventeen Modern Programming Languages, 2nd ed. 26

Example public class OutOfGas extends Exception { public OutOfGas(String details) { super(details); This calls a base-class constructor to initialize the field returned by getmessage(). try { throw new OutOfGas("You have run out of gas."); catch (OutOfGas e) { System.out.println(e.getMessage()); Chapter Seventeen Modern Programming Languages, 2nd ed. 27

About super In Constructors The first statement in a constructor can be a call to super (with parameters, if needed) That calls a base class constructor Used to initialize inherited fields All constructors (except in Object) start with a call to another constructor if you don t include one, Java calls super() implicitly Chapter Seventeen Modern Programming Languages, 2nd ed. 28

More About Constructors Also, all classes have at least one constructor if you don t include one, Java provides a no-arg constructor implicitly public class OutOfGas extends Exception { public class OutOfGas extends Exception { public OutOfGas() { super(); These are equivalent! Chapter Seventeen Modern Programming Languages, 2nd ed. 29

public class OutOfGas extends Exception { private int miles; public OutOfGas(String details, int m) { super(details); miles = m; public int getmiles() { return miles; try { throw new OutOfGas("You have run out of gas.",19); catch (OutOfGas e) { System.out.println(e.getMessage()); System.out.println("Odometer: " + e.getmiles()); Chapter Seventeen Modern Programming Languages, 2nd ed. 30

Outline 17.2 Throwable classes 17.3 Catching exceptions 17.4 Throwing exceptions 17.5 Checked exceptions 17.6 Error handling 17.7 Finally 17.8 Farewell to Java Chapter Seventeen Modern Programming Languages, 2nd ed. 31

Checked Exceptions void z() { throw new OutOfGas("You have run out of gas.", 19"); This method will not compile: The exception OutOfGas is not handled Java has not complained about this in our previous examples why now? Java distinguishes between two kinds of exceptions: checked and unchecked Chapter Seventeen Modern Programming Languages, 2nd ed. 32

Throwable checked exceptions Error Exception unchecked exceptions RuntimeException The unchecked exceptions classes areerror and RuntimeException and their descendants. All others are checked. Chapter Seventeen Modern Programming Languages, 2nd ed. 33

What Gets Checked? A method that can get a checked exception is not permitted to ignore it It can catch it That is, the code that generates the exception can be inside a try statement with a catch part for that checked exception Or, it can declare that it does not catch it Using a throws clause Chapter Seventeen Modern Programming Languages, 2nd ed. 34

The Throws Clause void z() throws OutOfGas { throw new OutOfGas("You have run out of gas.", 19); A throws clause lists one or more throwable classes separated by commas This one always throws, but in general, the throws clause means might throw So any caller of z must catch OutOfGas, or place it in its own throws clause Chapter Seventeen Modern Programming Languages, 2nd ed. 35

If z declares that it throws OutOfGas then y must catch it, or declare it throws it too and so on all the way back to f z s activation record y s activation record... g s activation record f s activation record Chapter Seventeen Modern Programming Languages, 2nd ed. 36

Why Use Checked Exceptions The throws clause is like documentation: it tells the reader that this exception can result from a call of this method But it is verified documentation; if any checked exception can result from a method call, the compiler will insist it be declared This can make programs easier to read and more likely to be correct Chapter Seventeen Modern Programming Languages, 2nd ed. 37

How To Avoid Checked Exceptions You can always define your own exceptions using a different base class, such as Error or Throwable Then they will be unchecked Weigh the advantages carefully Chapter Seventeen Modern Programming Languages, 2nd ed. 38

Outline 17.2 Throwable classes 17.3 Catching exceptions 17.4 Throwing exceptions 17.5 Checked exceptions 17.6 Error handling 17.7 Finally 17.8 Farewell to Java Chapter Seventeen Modern Programming Languages, 2nd ed. 39

Handling Errors Example: popping an empty stack Techniques: Preconditions only Total definition Fatal errors Error flagging Using exceptions Chapter Seventeen Modern Programming Languages, 2nd ed. 40

Preconditions Only Document preconditions necessary to avoid errors Caller must ensure these are met, or explicitly check if not sure Chapter Seventeen Modern Programming Languages, 2nd ed. 41

/** * Pop the top int from this stack and return it. * This should be called only if the stack is * not empty. * @return the popped int */ public int pop() { Node n = top; top = n.getlink(); return n.getdata(); if (s.hasmore()) x = s.pop(); else Chapter Seventeen Modern Programming Languages, 2nd ed. 42

Drawbacks If the caller makes a mistake, and pops an empty stack: NullPointerException If that is uncaught, program crashes with an unhelpful error message If caught, program relies on undocumented internals; an implementation using an array would cause a different exception Chapter Seventeen Modern Programming Languages, 2nd ed. 43

Total Definition We can change the definition of pop so that it always works Define some standard behavior for popping an empty stack Like character-by-character file I/O in C: an EOF character at the end of the file Like IEEE floating-point: NaN and signed infinity results Chapter Seventeen Modern Programming Languages, 2nd ed. 44

/** * Pop the top int from this stack and return it. * If the stack is empty we return 0 and leave the * stack empty. * @return the popped int, or 0 if the stack is empty */ public int pop() { Node n = top; if (n==null) return 0; top = n.getlink(); return n.getdata(); Chapter Seventeen Modern Programming Languages, 2nd ed. 45

Drawbacks Can mask important problems If a client pops more than it pushes, this is probably a serious bug that should be detected and fixed, not concealed Chapter Seventeen Modern Programming Languages, 2nd ed. 46

Fatal Errors The old-fashioned approach: just crash! Preconditions, plus decisive action At least this does not conceal the problem Chapter Seventeen Modern Programming Languages, 2nd ed. 47

/** * Pop the top int from this stack and return it. * This should be called only if the stack is * not empty. If called when the stack is empty, * we print an error message and exit the program. * @return the popped int */ public int pop() { Node n = top; if (n==null) { System.out.println("Popping an empty stack!"); System.exit(-1); top = n.getlink(); return n.getdata(); Chapter Seventeen Modern Programming Languages, 2nd ed. 48

Drawbacks Not an object-oriented style: an object should do things to itself, not to the rest of the program Inflexible: different clients may want to handle the error differently Terminate Clean up and terminate Repair the error and continue Ignore the error Etc. Chapter Seventeen Modern Programming Languages, 2nd ed. 49

Error Flagging The method that detects the error can flag it somehow By returning a special value (like C malloc) By setting a global variable (like C errno) By setting an instance variable to be checked by a method call (like C ferror(f)) Caller must explicitly test for error Chapter Seventeen Modern Programming Languages, 2nd ed. 50

/** * Pop the top int from this stack and return it. * This should be called only if the stack is * not empty. If called when the stack is empty, * we set the error flag and return an undefined * value. * @return the popped int if stack not empty */ public int pop() { Node n = top; if (n==null) { error = true; return 0; top = n.getlink(); return n.getdata(); Chapter Seventeen Modern Programming Languages, 2nd ed. 51

/** * Return the error flag for this stack. The error * flag is set true if an empty stack is ever popped. * It can be reset to false by calling reseterror(). * @return the error flag */ public boolean geterror() { return error; /** * Reset the error flag. We set it to false. */ public void reseterror() { error = false; Chapter Seventeen Modern Programming Languages, 2nd ed. 52

/** * Pop the two top integers from the stack, divide * them, and push their integer quotient. There * should be at least two integers on the stack * when we are called. If not, we leave the stack * empty and set the error flag. */ public void divide() { int i = pop(); int j = pop(); if (geterror()) return; push(i/j); The kind of explicit error check required by an error flagging technique. Note that divide s caller may also have to check it, and its caller, and so on Chapter Seventeen Modern Programming Languages, 2nd ed. 53

Using Exceptions The method that first finds the error throws an exception May be checked or unchecked Part of the documented behavior of the method Chapter Seventeen Modern Programming Languages, 2nd ed. 54

/** * Pop the top int from this stack and return it. * @return the popped int * @exception EmptyStack if stack is empty */ public int pop() throws EmptyStack { Node n = top; if (n==null) throw new EmptyStack(); top = n.getlink(); return n.getdata(); Chapter Seventeen Modern Programming Languages, 2nd ed. 55

/** * Pop the two top integers from the stack, divide * them, and push their integer quotient. * @exception EmptyStack if stack runs out */ public void divide() throws EmptyStack { int i = pop(); int j = pop(); push(i/j); Caller makes no error check just passes the exception along if one occurs Chapter Seventeen Modern Programming Languages, 2nd ed. 56

Advantages Good error message even if uncaught Documented part of the interface Error caught right away, not masked Caller need not explicitly check for error Error can be ignored or handled flexibly Chapter Seventeen Modern Programming Languages, 2nd ed. 57

Outline 17.2 Throwable classes 17.3 Catching exceptions 17.4 Throwing exceptions 17.5 Checked exceptions 17.6 Error handling 17.7 Finally 17.8 Farewell to Java Chapter Seventeen Modern Programming Languages, 2nd ed. 58

The Full try Syntax <try-statement> ::= <try-part> <catch-parts> <try-part> <catch-parts> <finally-part> <try-part> <finally-part> <try-part> ::= try <compound-statement> <catch-parts> ::= <catch-part> <catch-parts> <catch-part> <catch-part> ::= catch (<type> <variable-name>) <compound-statement> <finally-part> ::= finally <compound-statement> There is an optional finally part No matter what happens, the finally part is always executed at the end of the try statement Chapter Seventeen Modern Programming Languages, 2nd ed. 59

Using finally file.open(); try { workwith(file); finally { file.close(); The finally part is usually used for cleanup operations Whether or not there is an exception, the file is closed Chapter Seventeen Modern Programming Languages, 2nd ed. 60

Example System.out.print("1"); try { System.out.print("2"); if (true) throw new Exception(); System.out.print("3"); catch (Exception e) { System.out.print("4"); finally { System.out.print("5"); System.out.println("6"); What does this print? What if we change new Exception() to new Throwable()? Chapter Seventeen Modern Programming Languages, 2nd ed. 61

Outline 17.2 Throwable classes 17.3 Catching exceptions 17.4 Throwing exceptions 17.5 Checked exceptions 17.6 Error handling 17.7 Finally 17.8 Farewell to Java Chapter Seventeen Modern Programming Languages, 2nd ed. 62

Parts We Skipped Fundamentals Primitive types: byte, short, long, float The enum type constructor for enumerations Various statements: do, for, break, continue, switch, assert Refinements Inner classes: define classes in any scope: inside other classes, in blocks, in expressions Generics: we saw only a quick peek Chapter Seventeen Modern Programming Languages, 2nd ed. 63

More Parts We Skipped Packages Classes are grouped into packages In many Java systems, the source files in a directory correspond to a package Default access (without public, private or protected) is package-wide Concurrency Synchronization constructs for multiple threads Parts of the API for creating threads Chapter Seventeen Modern Programming Languages, 2nd ed. 64

More Parts We Skipped The vast API containers (stacks, queues, hash tables, etc.) graphical user interfaces 2D and 3D graphics math pattern matching with regular expressions file IO network IO and XML encryption and security remote method invocation interfacing to databases and other tools Chapter Seventeen Modern Programming Languages, 2nd ed. 65