Fran Trees: AP CS Workshop1 Exceptions common exceptions throwing standard unchecked exceptions.

11
Fran Trees: AP CS Worksho p 1 Exceptions common exceptions throwing standard unchecked exceptions

Transcript of Fran Trees: AP CS Workshop1 Exceptions common exceptions throwing standard unchecked exceptions.

Page 1: Fran Trees: AP CS Workshop1 Exceptions common exceptions throwing standard unchecked exceptions.

Fran Trees: AP CS Workshop 1

Exceptions

common exceptions throwing standard

unchecked exceptions

Page 2: Fran Trees: AP CS Workshop1 Exceptions common exceptions throwing standard unchecked exceptions.

Fran Trees: AP CS Workshop 2

A Story…

You plan a trip to HersheyPark, with the primary purpose of riding the roller coasters. Just after you pay for admission and pull out the map to look for the first roller coaster, the sky turns dark, and you see lightning and hear thunder. An announcement on the loud-speaker says that the park is closing.

Page 3: Fran Trees: AP CS Workshop1 Exceptions common exceptions throwing standard unchecked exceptions.

Fran Trees: AP CS Workshop 3

A Story…CCJ AP CS Study Guide (Chapter

12)

An exceptional situation causes abrupt termination of the normal processing (the park visit). Note that throwing an exception doesn't tell you how to recover from the exceptional situation. An example of recovery would be if the management issued rain-checks. In Java, a catch clause is responsible for recovering from an exceptional condition.

Page 4: Fran Trees: AP CS Workshop1 Exceptions common exceptions throwing standard unchecked exceptions.

Fran Trees: AP CS Workshop 4

An exception is:

an "exceptional event"an event that occurs during program execution that disrupts normal flownot necessarily "an error."

Page 5: Fran Trees: AP CS Workshop1 Exceptions common exceptions throwing standard unchecked exceptions.

Fran Trees: AP CS Workshop 5

Some exceptions/errors

division by zeroaccessing out-of-bounds array elementattempting to open non-existent fileattempting to read beyond eof markerinvalid class castingspecifying negative array sizes

Page 6: Fran Trees: AP CS Workshop1 Exceptions common exceptions throwing standard unchecked exceptions.

Fran Trees: AP CS Workshop 6

Java Exception Hierarchy

Throwable

Error Exception

(unchecked)Runtime Exception

(checked)IOException

ArrayOutOfBoundsExceptionClassCastExceptionNullPointerException

NumberFormatExceptionInterruptedException

FileNotFoundException

Linkage Error

Page 7: Fran Trees: AP CS Workshop1 Exceptions common exceptions throwing standard unchecked exceptions.

Fran Trees: AP CS Workshop 7

Java Exception HierarchyChecked exceptions are compiler-

enforced exceptions. This means that when you call a method that throws a checked exception, you must tell the compiler what you are going to do about the exception if it is thrown. Checked exceptions are due to external circumstances that the programmer cannot prevent.

(NOT part of AP Subset)

Page 8: Fran Trees: AP CS Workshop1 Exceptions common exceptions throwing standard unchecked exceptions.

Fran Trees: AP CS Workshop 8

AP CS A Subset

common exceptionsthrowing standard UNCHECKED exceptions unchecked exceptions can be ignored by

the programmer; no need to use try/catch to handle the exception

not tested: try/catch

Page 9: Fran Trees: AP CS Workshop1 Exceptions common exceptions throwing standard unchecked exceptions.

Fran Trees: AP CS Workshop 9

Java Exception Hierarchy

Throwable

Error Exception

(unchecked)Runtime Exception

(checked)IOException

ArrayOutOfBoundsExceptionClassCastExceptionNullPointerException

NumberFormatExceptionInterruptedException

FileNotFOundException

Linkage Error

Page 10: Fran Trees: AP CS Workshop1 Exceptions common exceptions throwing standard unchecked exceptions.

Fran Trees: AP CS Workshop 10

AP CS A Subset

Students are expected to understand NullPointerException ArrayIndexOutOfBoundsException ArithmeticException ClassCastException

Students are expected to be able to throw IllegalStateException

signals that a method has been invoked at an illegal or inappropriate time.

NoSuchElementException if no more elements exist (useful with iterators)

Page 11: Fran Trees: AP CS Workshop1 Exceptions common exceptions throwing standard unchecked exceptions.

Fran Trees: AP CS Workshop 11

A look ahead…

public void add(Locatable obj){

Location loc = obj.location();if (!isEmpty(loc)){

throw new IllegalArgumentException("Location “+ loc + " is not a valid empty location");

}//Add object to the environment.theGrid[loc.row()][loc.col()]= obj;objectCount++;

}