cs 200 - programming i: exceptions · 2019-11-04 · cs200-programmingi:exceptions marcrenault...

60
CS 200 - Programming I: Exceptions Marc Renault Department of Computer Sciences University of Wisconsin – Madison Fall 2019 TopHat Sec 3 (1:20 PM) Join Code: 682357 TopHat Sec 4 (3:30 PM) Join Code: 296444

Upload: others

Post on 20-Jun-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

CS 200 - Programming I: Exceptions

Marc Renault

Department of Computer SciencesUniversity of Wisconsin – Madison

Fall 2019TopHat Sec 3 (1:20 PM) Join Code: 682357TopHat Sec 4 (3:30 PM) Join Code: 296444

Page 2: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

Command-Line Arguments

Page 3: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

Passing Command-Line Arguments

Example: java CmdLineEx arg0 agr1 arg2

Command-Line ArgumentsArguments passed to the program when it is launched.In example: arg0, arg1, arg2

String[] argsPassed to the main method via the String[] args.In example: String[] args = {"arg0","arg1","arg2"}.

1/30

Page 4: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

Passing Command-Line Arguments

Example: java CmdLineEx arg0 agr1 arg2

Command-Line ArgumentsArguments passed to the program when it is launched.In example: arg0, arg1, arg2

String[] argsPassed to the main method via the String[] args.In example: String[] args = {"arg0","arg1","arg2"}.

1/30

Page 5: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

TopHat Question 1

What is the output when executed using the commandjava CmdLineEx Do or do not, there is no try.

public class CmdLineEx {

public static void main(String [] args) {int i = 0;for(String s: args) {

i += s.length ();}System.out.print(i);

}

}

2/30

Page 6: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

Command-Line Argument Exercise

Write a program that receives from the command line both astring and action to perform (either changing to upper case orlower case). The command-line arguments are organized asfollows:

-s str , where str is the string to transform-l to lowercase-u to uppercase

There should only be one of -l or -u. The arguments should behandled in any order.After parsing the arguments, the program outputs the string,having converted to upper or lower case as per thecommand-line arguments.

3/30

Page 7: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

Exceptions

Page 8: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

Exceptions

What is an exception?Shorthand for “an exceptional event”.An event that interrupts the normal flow of the program.

What causes these events?

A failure of the machine where the program is running.A programming error.A user goes off the “happy path”.

Why use exceptions?Separates error-checking from normal “happy path” code.Organized with try-catch-finally and throws.

4/30

Page 9: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

Exceptions

What is an exception?Shorthand for “an exceptional event”.An event that interrupts the normal flow of the program.

What causes these events?A failure of the machine where the program is running.

A programming error.A user goes off the “happy path”.

Why use exceptions?Separates error-checking from normal “happy path” code.Organized with try-catch-finally and throws.

4/30

Page 10: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

Exceptions

What is an exception?Shorthand for “an exceptional event”.An event that interrupts the normal flow of the program.

What causes these events?A failure of the machine where the program is running.A programming error.

A user goes off the “happy path”.

Why use exceptions?Separates error-checking from normal “happy path” code.Organized with try-catch-finally and throws.

4/30

Page 11: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

Exceptions

What is an exception?Shorthand for “an exceptional event”.An event that interrupts the normal flow of the program.

What causes these events?A failure of the machine where the program is running.A programming error.A user goes off the “happy path”.

Why use exceptions?Separates error-checking from normal “happy path” code.Organized with try-catch-finally and throws.

4/30

Page 12: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

Exceptions

What is an exception?Shorthand for “an exceptional event”.An event that interrupts the normal flow of the program.

What causes these events?A failure of the machine where the program is running.A programming error.A user goes off the “happy path”.

Why use exceptions?Separates error-checking from normal “happy path” code.Organized with try-catch-finally and throws.

4/30

Page 13: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

Java Exception HierarchyObject

Throwable

Error

...

Exception

Runtime

...

...

5/30

Page 14: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

Errors

CausesErrors are caused by abnormal conditions beyond the control ofthe programmer causing the program to fail such as hardwarefailures.

Some Examplesjava.io.IOError

java.lang.OutOfMemoryError

java.lang.LinkageError

6/30

Page 15: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

Java Exception HierarchyObject

Throwable

Error

...

Exception

Runtime

...

...

7/30

Page 16: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

Runtime Exception

CausesRuntime Exceptions are typically caused by programmingerrors (bugs) not caught at compile time such as trying toaccess an out of bounds cell in an array.

Some Examplesjava.lang.NullPointerExceptionjava.lang.IndexOutOfBoundsException

java.lang.ArrayIndexOutOfBoundsExceptionjava.lang.StringIndexOutOfBoundsException

java.lang.ArithmeticException

8/30

Page 17: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

Java Exception HierarchyObject

Throwable

Error

...

Exception

Runtime

...

...

9/30

Page 18: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

Java Exception Hierarchy

Unchecked

Object

Throwable

Error

...

Exception

Runtime

...

...

9/30

Page 19: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

Java Exception Hierarchy

Unchecked

Checked

Object

Throwable

Error

...

Exception

Runtime

...

...

9/30

Page 20: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

Unchecked vs Checked

Unchecked ExceptionAn unchecked exception is an exceptional condition that theapplication usually cannot anticipate or recover from. Theseare:

ErrorsRuntime exceptions

Checked ExceptionsAll other exceptions are checked exceptions. A method musthandle all checked exceptions by either:

Using a try-catch block, orThrowing the exception.

10/30

Page 21: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

Unchecked vs Checked

Unchecked ExceptionAn unchecked exception is an exceptional condition that theapplication usually cannot anticipate or recover from. Theseare:

ErrorsRuntime exceptions

Checked ExceptionsAll other exceptions are checked exceptions. A method musthandle all checked exceptions by either:

Using a try-catch block, orThrowing the exception.

10/30

Page 22: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

TopHat Question 2What kind of exception isjava.util.InputMismatchException?The following code compiles. When the user inputs “a”, theprogram terminates with ajava.util.InputMismatchException.import java.util.Scanner;

public class ExceptionEx1 {

public static void main(String [] arg) {Scanner sc = new Scanner(System.in);int a = sc.nextInt ();System.out.println("Value is " + a);

}

}11/30

Page 23: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

try-catch

try {tryStmt1;...tryStmtN;

}catch (AnException e) {

catchStmt1;...catchStmtN;

}

Control Flow

Try Stmt Block

AnException?

AnExceptionCatch Block

Followingstatements

Yes

No

12/30

Page 24: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

try-catch

try {tryStmt1;...tryStmtN;

}catch (AnException e) {

catchStmt1;...catchStmtN;

}

Control Flow

Try Stmt Block

AnException?

AnExceptionCatch Block

Followingstatements

Yes

No

12/30

Page 25: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

TopHat Question 3

What is the output when the user enters “z”?

Scanner sc = new Scanner(System.in);try {

int a = sc.nextInt ();System.out.println("Value is " + a);

}catch(Exception e) {

System.out.print("Catch.");}System.out.print("Done.");

13/30

Page 26: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

Catching the Exact Exception

Scanner sc = new Scanner(System.in);try {

int a = sc.nextInt ();System.out.println("Value is " + a);

}catch(Exception e) {

System.out.print("Catch.");}

Best PracticeWhen catching exceptions, be as precise as possible about theexception.

14/30

Page 27: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

Catching the Exact Exception

Scanner sc = new Scanner(System.in);try {

int a = sc.nextInt ();System.out.println("Value is " + a);

}catch(InputMismatchException e) {

System.out.print("Input was not an integer.");}

Best PracticeWhen catching exceptions, be as precise as possible about theexception.

14/30

Page 28: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

Stack Trace

Scanner sc = new Scanner(System.in);try {

int a = sc.nextInt ();System.out.println("Value is " + a);

}catch(InputMismatchException e) {

System.out.println("Input was not an integer.");e.printStackTrace ();

}

Stack TracePrints the trace of methods calls (with line numbers) onthe stack leading to the exception.Throwable has an instance method printStackTrace().

15/30

Page 29: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

Basic try-catch-finally

try {tryStatements;

}catch (AnException e) {

catchStatements;}finally {

finallyStatements;}

Basic Control Flow

Try Stmt Block

AnException?

AnExceptionCatch Block

Finally Stmt Block

Followingstatements

Yes

No

16/30

Page 30: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

Basic try-catch-finally

try {tryStatements;

}catch (AnException e) {

catchStatements;}finally {

finallyStatements;}

Basic Control Flow

Try Stmt Block

AnException?

AnExceptionCatch Block

Finally Stmt Block

Followingstatements

Yes

No

16/30

Page 31: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

TopHat Question 4

What is the output when the user enters “z”?

Scanner sc = new Scanner(System.in);try {

int a = sc.nextInt ();System.out.print("Value is " + a + ".");

}catch(InputMismatchException e) {

System.out.print("Catch.");}finally {

System.out.print("Finally.");}System.out.print("Done.");

17/30

Page 32: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

TopHat Question 5

What is the output when the user enters “2”?

Scanner sc = new Scanner(System.in);try {

int a = sc.nextInt ();System.out.print("Value is " + a + ".");

}catch(InputMismatchException e) {

System.out.print("Catch.");}finally {

System.out.print("Finally.");}System.out.print("Done.");

18/30

Page 33: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

Multiple catchtry {

tryStatements;}catch (Exception1 e) {

catch1Statements;}catch (Exception2 e) {

catch1Statements;}...catch (ExceptionN e) {

}

Control Flow

Try Stmt Block

CaughtException?

Exception1Catch Block

ExceptionNCatch Block

Followingstatements

No

19/30

Page 34: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

Multiple catchtry {

tryStatements;}catch (Exception1 e) {

catch1Statements;}catch (Exception2 e) {

catch1Statements;}...catch (ExceptionN e) {

}

Control Flow

Try Stmt Block

CaughtException?

Exception1Catch Block

ExceptionNCatch Block

Followingstatements

No

19/30

Page 35: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

TopHat Question 6What is the output when the user enters “0”?

Scanner sc = new Scanner(System.in);try {

int a = sc.nextInt ();System.out.print("Value is " + 10/a + ".");

}catch(InputMismatchException e) {

System.out.print("Catch Mismatch.");}catch(ArithmeticException e) {

System.out.print("Catch Div 0.");}finally {

System.out.print("Finally.");}System.out.print("Done.");

20/30

Page 36: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

Advanced try-catch-finally

try {tryStatements;

}catch (AnException e) {

catchStatements;}finally {

finallyStatements;}

What if the catch blockthrows an exception?

The finally block is stillexecuted.

What if the catch blockreturns?The finally block is stillexecuted.

What if the try blockreturns?The finally block is stillexecuted.

What if the try block throwsan uncaught exception?The finally block is stillexecuted.

21/30

Page 37: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

Advanced try-catch-finally

try {tryStatements;

}catch (AnException e) {

catchStatements;}finally {

finallyStatements;}

What if the catch blockthrows an exception?The finally block is stillexecuted.

What if the catch blockreturns?The finally block is stillexecuted.

What if the try blockreturns?The finally block is stillexecuted.

What if the try block throwsan uncaught exception?The finally block is stillexecuted.

21/30

Page 38: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

Advanced try-catch-finally

try {tryStatements;

}catch (AnException e) {

catchStatements;}finally {

finallyStatements;}

What if the catch blockthrows an exception?The finally block is stillexecuted.

What if the catch blockreturns?

The finally block is stillexecuted.

What if the try blockreturns?The finally block is stillexecuted.

What if the try block throwsan uncaught exception?The finally block is stillexecuted.

21/30

Page 39: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

Advanced try-catch-finally

try {tryStatements;

}catch (AnException e) {

catchStatements;}finally {

finallyStatements;}

What if the catch blockthrows an exception?The finally block is stillexecuted.

What if the catch blockreturns?The finally block is stillexecuted.

What if the try blockreturns?The finally block is stillexecuted.

What if the try block throwsan uncaught exception?The finally block is stillexecuted.

21/30

Page 40: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

Advanced try-catch-finally

try {tryStatements;

}catch (AnException e) {

catchStatements;}finally {

finallyStatements;}

What if the catch blockthrows an exception?The finally block is stillexecuted.

What if the catch blockreturns?The finally block is stillexecuted.

What if the try blockreturns?

The finally block is stillexecuted.

What if the try block throwsan uncaught exception?The finally block is stillexecuted.

21/30

Page 41: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

Advanced try-catch-finally

try {tryStatements;

}catch (AnException e) {

catchStatements;}finally {

finallyStatements;}

What if the catch blockthrows an exception?The finally block is stillexecuted.

What if the catch blockreturns?The finally block is stillexecuted.

What if the try blockreturns?The finally block is stillexecuted.

What if the try block throwsan uncaught exception?The finally block is stillexecuted.

21/30

Page 42: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

Advanced try-catch-finally

try {tryStatements;

}catch (AnException e) {

catchStatements;}finally {

finallyStatements;}

What if the catch blockthrows an exception?The finally block is stillexecuted.

What if the catch blockreturns?The finally block is stillexecuted.

What if the try blockreturns?The finally block is stillexecuted.

What if the try block throwsan uncaught exception?

The finally block is stillexecuted.

21/30

Page 43: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

Advanced try-catch-finally

try {tryStatements;

}catch (AnException e) {

catchStatements;}finally {

finallyStatements;}

What if the catch blockthrows an exception?The finally block is stillexecuted.

What if the catch blockreturns?The finally block is stillexecuted.

What if the try blockreturns?The finally block is stillexecuted.

What if the try block throwsan uncaught exception?The finally block is stillexecuted.

21/30

Page 44: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

TopHat Question 7

What is the output when the user enters “z”?

Scanner sc = new Scanner(System.in);try {

int a = sc.nextInt ();System.out.print("Value is " + a + ".");return;

}catch(InputMismatchException e) {

System.out.print("Catch.");return;

}finally {

System.out.print("Finally.");}

22/30

Page 45: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

Basic Throwing ExceptionsCreating and Using Exceptions

Constructors:Exception() – Creates an Exception object.Exception(msg) – Creates an Exception object containingthe message msg.

Some methods:printStackTrace() – Prints the exception stack trace.getMessage() – Returns the message associated with theexception.

Throwing an Exceptionthrow aThrowable;

Stops normal executions and throws an exception.E.g.throw new Exception("An exception!");

23/30

Page 46: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

Basic Throwing ExceptionsCreating and Using Exceptions

Constructors:Exception() – Creates an Exception object.Exception(msg) – Creates an Exception object containingthe message msg.

Some methods:printStackTrace() – Prints the exception stack trace.getMessage() – Returns the message associated with theexception.

Throwing an Exceptionthrow aThrowable;

Stops normal executions and throws an exception.E.g.throw new Exception("An exception!");

23/30

Page 47: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

Basic Throwing ExceptionsCreating and Using Exceptions

Constructors:Exception() – Creates an Exception object.Exception(msg) – Creates an Exception object containingthe message msg.

Some methods:printStackTrace() – Prints the exception stack trace.getMessage() – Returns the message associated with theexception.

Throwing an Exceptionthrow aThrowable;

Stops normal executions and throws an exception.E.g.throw new Exception("An exception!");

23/30

Page 48: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

TopHat Question 8What is the output when the user enters “z”?Scanner sc = new Scanner(System.in);try {

int a = sc.nextInt ();if(a <= 0)

throw new Exception("Error: Val <= 0.");System.out.print("Value is " + 10/a + ".");

}catch(InputMismatchException e) {

System.out.print("Catch Mismatch.");}catch(Exception e) {

System.out.print(e.getMessage ());}finally {

System.out.print("Finally.");}System.out.print("Done.");

24/30

Page 49: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

TopHat Question 9What is the output when the user enters “0”?Scanner sc = new Scanner(System.in);try {

int a = sc.nextInt ();if(a <= 0)

throw new Exception("Error: Val <= 0.");System.out.print("Value is " + 10/a + ".");

}catch(InputMismatchException e) {

System.out.print("Catch Mismatch.");}catch(Exception e) {

System.out.print(e.getMessage ());}finally {

System.out.print("Finally.");}System.out.print("Done.");

25/30

Page 50: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

Methods Throwing ExceptionsSpecifying Exceptions Thrown... someMethod(...) throws exception1,exception2,...

Part of the method header.All checked exceptions must be listed.Unchecked ones are optional.

Throwing vs CatchingA method, someMethod, that throws a checked exception ispassing the exception to the caller.The caller can either:

Throw the exception further up the stack calls, orPut the method call to someMethod in a try block andhandle the exception in a catch block.

26/30

Page 51: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

Methods Throwing ExceptionsSpecifying Exceptions Thrown... someMethod(...) throws exception1,exception2,...

Part of the method header.All checked exceptions must be listed.Unchecked ones are optional.

Throwing vs CatchingA method, someMethod, that throws a checked exception ispassing the exception to the caller.

The caller can either:Throw the exception further up the stack calls, orPut the method call to someMethod in a try block andhandle the exception in a catch block.

26/30

Page 52: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

Methods Throwing ExceptionsSpecifying Exceptions Thrown... someMethod(...) throws exception1,exception2,...

Part of the method header.All checked exceptions must be listed.Unchecked ones are optional.

Throwing vs CatchingA method, someMethod, that throws a checked exception ispassing the exception to the caller.The caller can either:

Throw the exception further up the stack calls, or

Put the method call to someMethod in a try block andhandle the exception in a catch block.

26/30

Page 53: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

Methods Throwing ExceptionsSpecifying Exceptions Thrown... someMethod(...) throws exception1,exception2,...

Part of the method header.All checked exceptions must be listed.Unchecked ones are optional.

Throwing vs CatchingA method, someMethod, that throws a checked exception ispassing the exception to the caller.The caller can either:

Throw the exception further up the stack calls, orPut the method call to someMethod in a try block andhandle the exception in a catch block.

26/30

Page 54: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

TopHat Question 10What is the output when the user enters “0”?

public static void div(int a, int b) throws Exception {if(b == 0)

throw new Exception("Error: a is 0.");System.out.print("Value is " + b/a + ".");

}

public static void main(String [] arg) {Scanner sc = new Scanner(System.in);int a = sc.nextInt ();try {

div(10, a);}catch(Exception e) {

System.out.print(e.getMessage ());}System.out.print("Done.");

}27/30

Page 55: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

TopHat Question 11What is the output when the user enters “z”?

public static void div(int a, int b) throws Exception {if(b == 0)

throw new Exception("Error: a is 0.");System.out.print("Value is " + b/a + ".");

}

public static void main(String [] arg) {Scanner sc = new Scanner(System.in);int a = sc.nextInt ();try {

div(10, a);}catch(Exception e) {

System.out.print(e.getMessage ());}System.out.print("Done.");

}28/30

Page 56: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

TopHat Question 12What is the stack trace output when the program runs?

1 public class ExceptionEx11 {23 public static void f() throws Exception {4 throw new Exception ();5 }67 public static void main(String [] arg) {8 try {9 g();

10 }11 catch(Exception e) {12 e.printStackTrace ();13 }14 }1516 public static void g() throws Exception {17 f();18 }1920 }

29/30

Page 57: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Command-Line Arguments Exceptions

Further Reading

COMP SCI 200: Programming IzyBooks.com, 2015.zyBook code:WISCCOMPSCI200Fall2019

Chapter 10. Exceptions

30/30

Page 58: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Appendix References

Appendix

Page 59: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Appendix References

References

Page 60: CS 200 - Programming I: Exceptions · 2019-11-04 · CS200-ProgrammingI:Exceptions MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357

Appendix References

Image Sources I

https://brand.wisc.edu/web/logos/

http://www.zybooks.com/

31/30