200 errors found: error: /u/testgq.java:314: compareto ...• next need to nd in your code where...

106

Upload: others

Post on 16-Jul-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Lecture 9

200 errors found:Error: /U/TestGQ.java:314: compareTo(GQueue<java.lang.Integer>) inGQueue<java.lang.Integer> cannot be applied to (GQueue<java.lang.String>)...

Summer 2013

M. Jason HinekCarleton University

Page 2: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Day 9

· a quick look back (day 8)

· assignments

· exceptions and exception handling

· throws· try and catch· finally· throw· read Chapter 9 in the Lanthier notes

· a quick look ahead (day 10)

2 / 28

Page 3: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Reminders

Notesif you have been taking (good) notes so far...

get some recognition from the University...

help out a fellow student... email

[email protected] with your name, studentnumber and course code

Final ExamAugust 19, 201319:00

3 / 28

Page 4: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Last time...

The material in Day 8 touched on

· truth and lies about compareTo()

· generics· the ? woldcard for types· restricting types

· wrapper classes· one corresponding to each primitive data type

· method chaining· good or bad?

4 / 28

Page 5: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Bugs...

often use the word bug when there is a problem with our program

· bug is an error in our code (or hardware)

· debugging is a methodical process of �nding and reducing thenumber of bugs

5 / 28

Page 6: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Bugs...

· bug is an error in our code (or hardware)

· debugging is a methodical process of �nding and reducing thenumber of bugs

5 / 28

Page 7: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Bugs...

· bug is an error in our code (or hardware)

· debugging is a methodical process of �nding and reducing thenumber of bugs

5 / 28

Page 8: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Bugs...

· bug is an error in our code (or hardware)

· debugging is a methodical process of �nding and reducing thenumber of bugs

5 / 28

Page 9: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Bugs...

essentially 3 types of bugs/errors

· compile errors

· program does not compile but compiler tells you why· syntax errors, type mismatch errors, ...· these are your fault

· runtime errors

· cannot be determined at compile time· sometimes require re-design of code· these may be your fault

· logical errors

· non-syntax mistakes in your code· Java cannot detect and explain these errors· may be very hard to �nd...· these are definitely your fault

6 / 28

Page 10: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Bugs...

essentially 3 types of bugs/errors

· compile errors

· program does not compile but compiler tells you why· syntax errors, type mismatch errors, ...· these are your fault

· runtime errors

· cannot be determined at compile time· sometimes require re-design of code· these may be your fault

· logical errors

· non-syntax mistakes in your code· Java cannot detect and explain these errors· may be very hard to �nd...· these are definitely your fault

6 / 28

Page 11: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Bugs...

essentially 3 types of bugs/errors

· compile errors

· program does not compile but compiler tells you why· syntax errors, type mismatch errors, ...

· these are your fault

· runtime errors

· cannot be determined at compile time· sometimes require re-design of code· these may be your fault

· logical errors

· non-syntax mistakes in your code· Java cannot detect and explain these errors· may be very hard to �nd...· these are definitely your fault

6 / 28

Page 12: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Bugs...

essentially 3 types of bugs/errors

· compile errors

· program does not compile but compiler tells you why· syntax errors, type mismatch errors, ...

· these are your fault

· runtime errors

· cannot be determined at compile time· sometimes require re-design of code

· these may be your fault

· logical errors

· non-syntax mistakes in your code· Java cannot detect and explain these errors· may be very hard to �nd...· these are definitely your fault

6 / 28

Page 13: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Bugs...

essentially 3 types of bugs/errors

· compile errors

· program does not compile but compiler tells you why· syntax errors, type mismatch errors, ...

· these are your fault

· runtime errors

· cannot be determined at compile time· sometimes require re-design of code

· these may be your fault

· logical errors

· non-syntax mistakes in your code· Java cannot detect and explain these errors· may be very hard to �nd...

· these are definitely your fault

6 / 28

Page 14: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Bugs...

essentially 3 types of bugs/errors

· compile errors

· program does not compile but compiler tells you why· syntax errors, type mismatch errors, ...· these are your fault

· runtime errors

· cannot be determined at compile time· sometimes require re-design of code

· these may be your fault

· logical errors

· non-syntax mistakes in your code· Java cannot detect and explain these errors· may be very hard to �nd...

· these are definitely your fault

6 / 28

Page 15: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Bugs...

essentially 3 types of bugs/errors

· compile errors

· program does not compile but compiler tells you why· syntax errors, type mismatch errors, ...· these are your fault

· runtime errors

· cannot be determined at compile time· sometimes require re-design of code· these may be your fault

· logical errors

· non-syntax mistakes in your code· Java cannot detect and explain these errors· may be very hard to �nd...

· these are definitely your fault

6 / 28

Page 16: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Bugs...

essentially 3 types of bugs/errors

· compile errors

· program does not compile but compiler tells you why· syntax errors, type mismatch errors, ...· these are your fault

· runtime errors

· cannot be determined at compile time· sometimes require re-design of code· these may be your fault

· logical errors

· non-syntax mistakes in your code· Java cannot detect and explain these errors· may be very hard to �nd...· these are definitely your fault

6 / 28

Page 17: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Bugs...

· �rst need to �nd them

· compile errors are found for free?

· test, test, test, ...· println is your friend

· next need to �nd in your code where they occur

· JVM might tell you where code crashed (runtime error)· observable error may have been caused elsewhere though...· play computer! backtrack...· might be easy or hard

· next we �x the bug

· if we know exactly where the bugs originates, often easy to �x· serious bugs may require rethinking your code/objects

7 / 28

Page 18: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Bugs...

· �rst need to �nd them

· compile errors are found for free?

· test, test, test, ...· println is your friend

· next need to �nd in your code where they occur

· JVM might tell you where code crashed (runtime error)· observable error may have been caused elsewhere though...· play computer! backtrack...· might be easy or hard

· next we �x the bug

· if we know exactly where the bugs originates, often easy to �x· serious bugs may require rethinking your code/objects

7 / 28

Page 19: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Bugs...

· �rst need to �nd them· compile errors are found for free?

· test, test, test, ...· println is your friend

· next need to �nd in your code where they occur

· JVM might tell you where code crashed (runtime error)· observable error may have been caused elsewhere though...· play computer! backtrack...· might be easy or hard

· next we �x the bug

· if we know exactly where the bugs originates, often easy to �x· serious bugs may require rethinking your code/objects

7 / 28

Page 20: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Bugs...

· �rst need to �nd them· compile errors are found for free?

· test, test, test, ...

· println is your friend

· next need to �nd in your code where they occur

· JVM might tell you where code crashed (runtime error)· observable error may have been caused elsewhere though...· play computer! backtrack...· might be easy or hard

· next we �x the bug

· if we know exactly where the bugs originates, often easy to �x· serious bugs may require rethinking your code/objects

7 / 28

Page 21: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Bugs...

· �rst need to �nd them· compile errors are found for free?

· test, test, test, ...· println is your friend

· next need to �nd in your code where they occur

· JVM might tell you where code crashed (runtime error)· observable error may have been caused elsewhere though...· play computer! backtrack...· might be easy or hard

· next we �x the bug

· if we know exactly where the bugs originates, often easy to �x· serious bugs may require rethinking your code/objects

7 / 28

Page 22: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Bugs...

· �rst need to �nd them· compile errors are found for free?

· test, test, test, ...· println is your friend

· next need to �nd in your code where they occur

· JVM might tell you where code crashed (runtime error)· observable error may have been caused elsewhere though...· play computer! backtrack...· might be easy or hard

· next we �x the bug

· if we know exactly where the bugs originates, often easy to �x· serious bugs may require rethinking your code/objects

7 / 28

Page 23: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Bugs...

· �rst need to �nd them· compile errors are found for free?

· test, test, test, ...· println is your friend

· next need to �nd in your code where they occur· JVM might tell you where code crashed (runtime error)

· observable error may have been caused elsewhere though...· play computer! backtrack...· might be easy or hard

· next we �x the bug

· if we know exactly where the bugs originates, often easy to �x· serious bugs may require rethinking your code/objects

7 / 28

Page 24: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Bugs...

· �rst need to �nd them· compile errors are found for free?

· test, test, test, ...· println is your friend

· next need to �nd in your code where they occur· JVM might tell you where code crashed (runtime error)· observable error may have been caused elsewhere though...

· play computer! backtrack...· might be easy or hard

· next we �x the bug

· if we know exactly where the bugs originates, often easy to �x· serious bugs may require rethinking your code/objects

7 / 28

Page 25: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Bugs...

· �rst need to �nd them· compile errors are found for free?

· test, test, test, ...· println is your friend

· next need to �nd in your code where they occur· JVM might tell you where code crashed (runtime error)· observable error may have been caused elsewhere though...· play computer! backtrack...

· might be easy or hard

· next we �x the bug

· if we know exactly where the bugs originates, often easy to �x· serious bugs may require rethinking your code/objects

7 / 28

Page 26: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Bugs...

· �rst need to �nd them· compile errors are found for free?

· test, test, test, ...· println is your friend

· next need to �nd in your code where they occur· JVM might tell you where code crashed (runtime error)· observable error may have been caused elsewhere though...· play computer! backtrack...· might be easy or hard

· next we �x the bug

· if we know exactly where the bugs originates, often easy to �x· serious bugs may require rethinking your code/objects

7 / 28

Page 27: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Bugs...

· �rst need to �nd them· compile errors are found for free?

· test, test, test, ...· println is your friend

· next need to �nd in your code where they occur· JVM might tell you where code crashed (runtime error)· observable error may have been caused elsewhere though...· play computer! backtrack...· might be easy or hard

· next we �x the bug

· if we know exactly where the bugs originates, often easy to �x· serious bugs may require rethinking your code/objects

7 / 28

Page 28: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Bugs...

· �rst need to �nd them· compile errors are found for free?

· test, test, test, ...· println is your friend

· next need to �nd in your code where they occur· JVM might tell you where code crashed (runtime error)· observable error may have been caused elsewhere though...· play computer! backtrack...· might be easy or hard

· next we �x the bug· if we know exactly where the bugs originates, often easy to �x

· serious bugs may require rethinking your code/objects

7 / 28

Page 29: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Bugs...

· �rst need to �nd them· compile errors are found for free?

· test, test, test, ...· println is your friend

· next need to �nd in your code where they occur· JVM might tell you where code crashed (runtime error)· observable error may have been caused elsewhere though...· play computer! backtrack...· might be easy or hard

· next we �x the bug· if we know exactly where the bugs originates, often easy to �x· serious bugs may require rethinking your code/objects

7 / 28

Page 30: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Bugs...

part of the problem is that we don't live in a perfect world...

· people do not follow API speci�cations (preconditions)

· people do weird things with your code...

· �les get corrupted

you have no control over how people use your codeyou have no control over the universe your code is running in

· need to write robust code

8 / 28

Page 31: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Bugs...

part of the problem is that we don't live in a perfect world...

· people do not follow API speci�cations (preconditions)

· people do weird things with your code...

· �les get corrupted

you have no control over how people use your codeyou have no control over the universe your code is running in

· need to write robust code

8 / 28

Page 32: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Bugs...

part of the problem is that we don't live in a perfect world...

· people do not follow API speci�cations (preconditions)

· people do weird things with your code...

· �les get corrupted

you have no control over how people use your codeyou have no control over the universe your code is running in

· need to write robust code

8 / 28

Page 33: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Bugs...

part of the problem is that we don't live in a perfect world...

· people do not follow API speci�cations (preconditions)

· people do weird things with your code...

· �les get corrupted

you have no control over how people use your codeyou have no control over the universe your code is running in

· need to write robust code

8 / 28

Page 34: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Bugs...

part of the problem is that we don't live in a perfect world...

· people do not follow API speci�cations (preconditions)

· people do weird things with your code...

· �les get corrupted

you have no control over how people use your codeyou have no control over the universe your code is running in

· need to write robust code

8 / 28

Page 35: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Bugs...

part of the problem is that we don't live in a perfect world...

· people do not follow API speci�cations (preconditions)

· people do weird things with your code...

· �les get corrupted

you have no control over how people use your codeyou have no control over the universe your code is running in

· need to write robust code

8 / 28

Page 36: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Bugs...

we could write code to check for all possibilities in our code

· error-checking is code added to your code to look for bad data

· error-handling is what you do when bad data is found

in Java we use Exceptions...

· an Exception is an error that occurs in your code

· Exception Handling is what you do when an exception is found

goal of using exceptions

· you want your program to die gracefully

· you want to program to recover from bad data

9 / 28

Page 37: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Bugs...

we could write code to check for all possibilities in our code

· error-checking is code added to your code to look for bad data

· error-handling is what you do when bad data is found

in Java we use Exceptions...

· an Exception is an error that occurs in your code

· Exception Handling is what you do when an exception is found

goal of using exceptions

· you want your program to die gracefully

· you want to program to recover from bad data

9 / 28

Page 38: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Bugs...

we could write code to check for all possibilities in our code

· error-checking is code added to your code to look for bad data

· error-handling is what you do when bad data is found

in Java we use Exceptions...

· an Exception is an error that occurs in your code

· Exception Handling is what you do when an exception is found

goal of using exceptions

· you want your program to die gracefully

· you want to program to recover from bad data

9 / 28

Page 39: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Bugs...

we could write code to check for all possibilities in our code

· error-checking is code added to your code to look for bad data

· error-handling is what you do when bad data is found

in Java we use Exceptions...

· an Exception is an error that occurs in your code

· Exception Handling is what you do when an exception is found

goal of using exceptions

· you want your program to die gracefully

· you want to program to recover from bad data

9 / 28

Page 40: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Bugs...

we could write code to check for all possibilities in our code

· error-checking is code added to your code to look for bad data

· error-handling is what you do when bad data is found

in Java we use Exceptions...

· an Exception is an error that occurs in your code

· Exception Handling is what you do when an exception is found

goal of using exceptions

· you want your program to die gracefully

· you want to program to recover from bad data

9 / 28

Page 41: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Bugs...

we could write code to check for all possibilities in our code

· error-checking is code added to your code to look for bad data

· error-handling is what you do when bad data is found

in Java we use Exceptions...

· an Exception is an error that occurs in your code

· Exception Handling is what you do when an exception is found

goal of using exceptions

· you want your program to die gracefully

· you want to program to recover from bad data

9 / 28

Page 42: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Bugs...

we could write code to check for all possibilities in our code

· error-checking is code added to your code to look for bad data

· error-handling is what you do when bad data is found

in Java we use Exceptions...

· an Exception is an error that occurs in your code

· Exception Handling is what you do when an exception is found

goal of using exceptions

· you want your program to die gracefully

· you want to program to recover from bad data

9 / 28

Page 43: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Bugs...

we could write code to check for all possibilities in our code

· error-checking is code added to your code to look for bad data

· error-handling is what you do when bad data is found

in Java we use Exceptions...

· an Exception is an error that occurs in your code

· Exception Handling is what you do when an exception is found

goal of using exceptions

· you want your program to die gracefully

· you want to program to recover from bad data

9 / 28

Page 44: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Bugs...

we could write code to check for all possibilities in our code

· error-checking is code added to your code to look for bad data

· error-handling is what you do when bad data is found

in Java we use Exceptions...

· an Exception is an error that occurs in your code

· Exception Handling is what you do when an exception is found

goal of using exceptions

· you want your program to die gracefully

· you want to program to recover from bad data

9 / 28

Page 45: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Exceptions...

in Java, exceptions are thrown

· the JVM automatically does this

· you explicitly do this in your code

in Java, thrown exceptions are always caught

· explicitly caught by your code

(graceful)

· delegated to someone else's code to be caught

(potentially graceful)

· caught by the JVM if everyone delegates

(ugly)

10 / 28

Page 46: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Exceptions...

in Java, exceptions are thrown

· the JVM automatically does this

· you explicitly do this in your code

in Java, thrown exceptions are always caught

· explicitly caught by your code

(graceful)

· delegated to someone else's code to be caught

(potentially graceful)

· caught by the JVM if everyone delegates

(ugly)

10 / 28

Page 47: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Exceptions...

in Java, exceptions are thrown

· the JVM automatically does this

· you explicitly do this in your code

in Java, thrown exceptions are always caught

· explicitly caught by your code

(graceful)

· delegated to someone else's code to be caught

(potentially graceful)

· caught by the JVM if everyone delegates

(ugly)

10 / 28

Page 48: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Exceptions...

in Java, exceptions are thrown

· the JVM automatically does this

· you explicitly do this in your code

in Java, thrown exceptions are always caught

· explicitly caught by your code

(graceful)

· delegated to someone else's code to be caught

(potentially graceful)

· caught by the JVM if everyone delegates

(ugly)

10 / 28

Page 49: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Exceptions...

in Java, exceptions are thrown

· the JVM automatically does this

· you explicitly do this in your code

in Java, thrown exceptions are always caught

· explicitly caught by your code

(graceful)

· delegated to someone else's code to be caught

(potentially graceful)

· caught by the JVM if everyone delegates

(ugly)

10 / 28

Page 50: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Exceptions...

in Java, exceptions are thrown

· the JVM automatically does this

· you explicitly do this in your code

in Java, thrown exceptions are always caught

· explicitly caught by your code

(graceful)

· delegated to someone else's code to be caught

(potentially graceful)

· caught by the JVM if everyone delegates

(ugly)

10 / 28

Page 51: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Exceptions...

in Java, exceptions are thrown

· the JVM automatically does this

· you explicitly do this in your code

in Java, thrown exceptions are always caught

· explicitly caught by your code

(graceful)

· delegated to someone else's code to be caught

(potentially graceful)

· caught by the JVM if everyone delegates

(ugly)

10 / 28

Page 52: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Exceptions...

in Java, exceptions are thrown

· the JVM automatically does this

· you explicitly do this in your code

in Java, thrown exceptions are always caught

· explicitly caught by your code (graceful)

· delegated to someone else's code to be caught (potentially graceful)

· caught by the JVM if everyone delegates (ugly)

10 / 28

Page 53: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Exceptions...

in Java, exceptions are objects

Error Class

· unrecoverable errors

· java.lang.StackOverflowError

· java.lang.OutOfMemoryError

· you do not generally catch these yourself

(you �x your code so it doesn't happen again!)

11 / 28

Page 54: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Exceptions...

in Java, exceptions are objects

Error Class

· unrecoverable errors

· java.lang.StackOverflowError

· java.lang.OutOfMemoryError

· you do not generally catch these yourself

(you �x your code so it doesn't happen again!)

11 / 28

Page 55: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Exceptions...

in Java, exceptions are objects

Error Class

· unrecoverable errors

· java.lang.StackOverflowError

· java.lang.OutOfMemoryError

· you do not generally catch these yourself

(you �x your code so it doesn't happen again!)

11 / 28

Page 56: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Exceptions...

in Java, exceptions are objects

Error Class

· unrecoverable errors· java.lang.StackOverflowError

· java.lang.OutOfMemoryError

· you do not generally catch these yourself

(you �x your code so it doesn't happen again!)

11 / 28

Page 57: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Exceptions...

in Java, exceptions are objects

Error Class

· unrecoverable errors· java.lang.StackOverflowError

· java.lang.OutOfMemoryError

· you do not generally catch these yourself

(you �x your code so it doesn't happen again!)

11 / 28

Page 58: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Exceptions...

in Java, exceptions are objects

Error Class

· unrecoverable errors· java.lang.StackOverflowError

· java.lang.OutOfMemoryError

· you do not generally catch these yourself(you �x your code so it doesn't happen again!)

11 / 28

Page 59: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Exceptions...

in Java, exceptions are objects

Exception Class

· less severe errors (we might be able to recover from these)

· there are checked and unchecked exceptions

· checked exceptions (Exception Class)

· compiler checks that these are explicitly caught(if there is a throw there must be a catch)

· IllegalAccessException (try to violate access modi�er)FileNotFoundException (�le is not found)

· unchecked exceptions (RuntimeException class)

· compiler does not check for a catch(typically left for JVM to crash program)

· you should rethink your code to avoid these...· ArtithmeticException (divide by zero for example)NullpointerException

IndexOutOfBoundsException

12 / 28

Page 60: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Exceptions...

in Java, exceptions are objects

Exception Class

· less severe errors (we might be able to recover from these)

· there are checked and unchecked exceptions

· checked exceptions (Exception Class)

· compiler checks that these are explicitly caught(if there is a throw there must be a catch)

· IllegalAccessException (try to violate access modi�er)FileNotFoundException (�le is not found)

· unchecked exceptions (RuntimeException class)

· compiler does not check for a catch(typically left for JVM to crash program)

· you should rethink your code to avoid these...· ArtithmeticException (divide by zero for example)NullpointerException

IndexOutOfBoundsException

12 / 28

Page 61: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Exceptions...

in Java, exceptions are objects

Exception Class

· less severe errors (we might be able to recover from these)

· there are checked and unchecked exceptions

· checked exceptions (Exception Class)

· compiler checks that these are explicitly caught(if there is a throw there must be a catch)

· IllegalAccessException (try to violate access modi�er)FileNotFoundException (�le is not found)

· unchecked exceptions (RuntimeException class)

· compiler does not check for a catch(typically left for JVM to crash program)

· you should rethink your code to avoid these...· ArtithmeticException (divide by zero for example)NullpointerException

IndexOutOfBoundsException

12 / 28

Page 62: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Exceptions...

in Java, exceptions are objects

Exception Class

· less severe errors (we might be able to recover from these)

· there are checked and unchecked exceptions

· checked exceptions (Exception Class)

· compiler checks that these are explicitly caught(if there is a throw there must be a catch)

· IllegalAccessException (try to violate access modi�er)FileNotFoundException (�le is not found)

· unchecked exceptions (RuntimeException class)

· compiler does not check for a catch(typically left for JVM to crash program)

· you should rethink your code to avoid these...· ArtithmeticException (divide by zero for example)NullpointerException

IndexOutOfBoundsException

12 / 28

Page 63: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Exceptions...

in Java, exceptions are objects

Exception Class

· less severe errors (we might be able to recover from these)

· there are checked and unchecked exceptions

· checked exceptions (Exception Class)· compiler checks that these are explicitly caught(if there is a throw there must be a catch)

· IllegalAccessException (try to violate access modi�er)FileNotFoundException (�le is not found)

· unchecked exceptions (RuntimeException class)

· compiler does not check for a catch(typically left for JVM to crash program)

· you should rethink your code to avoid these...· ArtithmeticException (divide by zero for example)NullpointerException

IndexOutOfBoundsException

12 / 28

Page 64: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Exceptions...

in Java, exceptions are objects

Exception Class

· less severe errors (we might be able to recover from these)

· there are checked and unchecked exceptions

· checked exceptions (Exception Class)· compiler checks that these are explicitly caught(if there is a throw there must be a catch)

· IllegalAccessException (try to violate access modi�er)FileNotFoundException (�le is not found)

· unchecked exceptions (RuntimeException class)

· compiler does not check for a catch(typically left for JVM to crash program)

· you should rethink your code to avoid these...· ArtithmeticException (divide by zero for example)NullpointerException

IndexOutOfBoundsException

12 / 28

Page 65: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Exceptions...

in Java, exceptions are objects

Exception Class

· less severe errors (we might be able to recover from these)

· there are checked and unchecked exceptions

· checked exceptions (Exception Class)· compiler checks that these are explicitly caught(if there is a throw there must be a catch)

· IllegalAccessException (try to violate access modi�er)FileNotFoundException (�le is not found)

· unchecked exceptions (RuntimeException class)

· compiler does not check for a catch(typically left for JVM to crash program)

· you should rethink your code to avoid these...· ArtithmeticException (divide by zero for example)NullpointerException

IndexOutOfBoundsException

12 / 28

Page 66: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Exceptions...

in Java, exceptions are objects

Exception Class

· less severe errors (we might be able to recover from these)

· there are checked and unchecked exceptions

· checked exceptions (Exception Class)· compiler checks that these are explicitly caught(if there is a throw there must be a catch)

· IllegalAccessException (try to violate access modi�er)FileNotFoundException (�le is not found)

· unchecked exceptions (RuntimeException class)· compiler does not check for a catch(typically left for JVM to crash program)

· you should rethink your code to avoid these...· ArtithmeticException (divide by zero for example)NullpointerException

IndexOutOfBoundsException

12 / 28

Page 67: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Exceptions...

in Java, exceptions are objects

Exception Class

· less severe errors (we might be able to recover from these)

· there are checked and unchecked exceptions

· checked exceptions (Exception Class)· compiler checks that these are explicitly caught(if there is a throw there must be a catch)

· IllegalAccessException (try to violate access modi�er)FileNotFoundException (�le is not found)

· unchecked exceptions (RuntimeException class)· compiler does not check for a catch(typically left for JVM to crash program)

· you should rethink your code to avoid these...

· ArtithmeticException (divide by zero for example)NullpointerException

IndexOutOfBoundsException

12 / 28

Page 68: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Exceptions...

in Java, exceptions are objects

Exception Class

· less severe errors (we might be able to recover from these)

· there are checked and unchecked exceptions

· checked exceptions (Exception Class)· compiler checks that these are explicitly caught(if there is a throw there must be a catch)

· IllegalAccessException (try to violate access modi�er)FileNotFoundException (�le is not found)

· unchecked exceptions (RuntimeException class)· compiler does not check for a catch(typically left for JVM to crash program)

· you should rethink your code to avoid these...· ArtithmeticException (divide by zero for example)NullpointerException

IndexOutOfBoundsException

12 / 28

Page 69: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Exceptions...

13 / 28

Page 70: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

let's take a break...for 5 minutes

14 / 28

Page 71: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Exceptions...

new Java keywords

· throws

· used in method declaration· says that this method is delegating this exception

· try/catch/finally

· used to execute code and handle exceptions thrown· "try" to execute this code..."catch" any thrown exceptions and handle them"�nally" execute some code after everything is done

· throw

· explicitly throw an exception

15 / 28

Page 72: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Exceptions...

new Java keywords

· throws

· used in method declaration· says that this method is delegating this exception

· try/catch/finally

· used to execute code and handle exceptions thrown· "try" to execute this code..."catch" any thrown exceptions and handle them"�nally" execute some code after everything is done

· throw

· explicitly throw an exception

15 / 28

Page 73: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Exceptions...

new Java keywords

· throws

· used in method declaration· says that this method is delegating this exception

· try/catch/finally

· used to execute code and handle exceptions thrown· "try" to execute this code..."catch" any thrown exceptions and handle them"�nally" execute some code after everything is done

· throw

· explicitly throw an exception

15 / 28

Page 74: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Exceptions...

new Java keywords

· throws

· used in method declaration· says that this method is delegating this exception

· try/catch/finally

· used to execute code and handle exceptions thrown· "try" to execute this code..."catch" any thrown exceptions and handle them"�nally" execute some code after everything is done

· throw

· explicitly throw an exception

15 / 28

Page 75: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Exceptions...

new Java keywords

· throws

· used in method declaration· says that this method is delegating this exception

· try/catch/finally· used to execute code and handle exceptions thrown

· "try" to execute this code..."catch" any thrown exceptions and handle them"�nally" execute some code after everything is done

· throw

· explicitly throw an exception

15 / 28

Page 76: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Exceptions...

new Java keywords

· throws

· used in method declaration· says that this method is delegating this exception

· try/catch/finally· used to execute code and handle exceptions thrown· "try" to execute this code..."catch" any thrown exceptions and handle them"�nally" execute some code after everything is done

· throw

· explicitly throw an exception

15 / 28

Page 77: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Exceptions...

new Java keywords

· throws

· used in method declaration· says that this method is delegating this exception

· try/catch/finally· used to execute code and handle exceptions thrown· "try" to execute this code..."catch" any thrown exceptions and handle them"�nally" execute some code after everything is done

· throw

· explicitly throw an exception

15 / 28

Page 78: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Exceptions...

new Java keywords

· throws

· used in method declaration· says that this method is delegating this exception

· try/catch/finally· used to execute code and handle exceptions thrown· "try" to execute this code..."catch" any thrown exceptions and handle them"�nally" execute some code after everything is done

· throw

· explicitly throw an exception

15 / 28

Page 79: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Throws

void openFile(String fname) throws java.io.FileNotFoundException{

...

}

tells the compiler that this method might

· explicitly throw a java.io.FileNotFoundException objector

calls a method that might

· explicitly throw a java.io.FileNotFoundException objectorcalls a method that might

· explicitly throw a java.io.FileNotFoundException object

or

calls a method that might

...

...

...

explicitly throw a java.io.FileNotFoundException object

16 / 28

Page 80: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Throws

void openFile(String fname) throws java.io.FileNotFoundException{

...

}

tells the compiler that this method might

· explicitly throw a java.io.FileNotFoundException objector

calls a method that might

· explicitly throw a java.io.FileNotFoundException objectorcalls a method that might

· explicitly throw a java.io.FileNotFoundException object

or

calls a method that might

...

...

...

explicitly throw a java.io.FileNotFoundException object

16 / 28

Page 81: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Throws

void openFile(String fname) throws java.io.FileNotFoundException{

...

}

tells the compiler that this method might

· explicitly throw a java.io.FileNotFoundException objectorcalls a method that might

· explicitly throw a java.io.FileNotFoundException objector

calls a method that might

· explicitly throw a java.io.FileNotFoundException object

or

calls a method that might

...

...

...

explicitly throw a java.io.FileNotFoundException object

16 / 28

Page 82: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Throws

void openFile(String fname) throws java.io.FileNotFoundException{

...

}

tells the compiler that this method might

· explicitly throw a java.io.FileNotFoundException objectorcalls a method that might

· explicitly throw a java.io.FileNotFoundException objector

calls a method that might

· explicitly throw a java.io.FileNotFoundException object

or

calls a method that might

...

...

...

explicitly throw a java.io.FileNotFoundException object

16 / 28

Page 83: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Throws

void openFile(String fname) throws java.io.FileNotFoundException{

...

}

tells the compiler that this method might

· explicitly throw a java.io.FileNotFoundException objectorcalls a method that might

· explicitly throw a java.io.FileNotFoundException objectorcalls a method that might

· explicitly throw a java.io.FileNotFoundException object

or

calls a method that might

...

...

...

explicitly throw a java.io.FileNotFoundException object

16 / 28

Page 84: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Throws

void openFile(String fname) throws java.io.FileNotFoundException{

...

}

tells the compiler that this method might

· explicitly throw a java.io.FileNotFoundException objectorcalls a method that might

· explicitly throw a java.io.FileNotFoundException objectorcalls a method that might

· explicitly throw a java.io.FileNotFoundException object

or

calls a method that might

...

...

...

explicitly throw a java.io.FileNotFoundException object

16 / 28

Page 85: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Throws

void openFile(String fname) throws java.io.FileNotFoundException{

...

}

tells the compiler that this method might

· explicitly throw a java.io.FileNotFoundException objectorcalls a method that might

· explicitly throw a java.io.FileNotFoundException objectorcalls a method that might

· explicitly throw a java.io.FileNotFoundException object

or

calls a method that might

...

...

...

explicitly throw a java.io.FileNotFoundException object

16 / 28

Page 86: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Throws

void openFile(String fname) throws java.io.FileNotFoundException{

...

}

tells the compiler that this method might

· explicitly throw a java.io.FileNotFoundException objectorcalls a method that might

· explicitly throw a java.io.FileNotFoundException objectorcalls a method that might

· explicitly throw a java.io.FileNotFoundException object

or

calls a method that might

...

...

...

explicitly throw a java.io.FileNotFoundException object

16 / 28

Page 87: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Throws

void openFile(String fname) throws java.io.FileNotFoundException{

...

}

let's look at an example

17 / 28

Page 88: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Try/Catch

· use throws to delegate exception handling

· use try and catch to handle the exception yourself

try{

block of code to try}

catch(Exception e){

block of codeto execute if exception is caught

}

catch(RuntimeException re){

block of codeto execute if exception is caught

}

18 / 28

Page 89: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Try/Catch

· use throws to delegate exception handling

· use try and catch to handle the exception yourself

try{

block of code to try}

catch(Exception e){

block of codeto execute if exception is caught

}

catch(RuntimeException re){

block of codeto execute if exception is caught

}

18 / 28

Page 90: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Try/Catch

· use throws to delegate exception handling

· use try and catch to handle the exception yourself

try{

block of code to try}

catch(Exception e){

block of codeto execute if exception is caught

}

catch(RuntimeException re){

block of codeto execute if exception is caught

}

18 / 28

Page 91: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Try/Catch

· use throws to delegate exception handling

· use try and catch to handle the exception yourself

let's look at the same example...

19 / 28

Page 92: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Try/Catch

order matters! class hierarchy matters!

java.lang.Object

↑java.lang.Throwable

↑java.lang.Exception

↑java.io.IOException

↑java.io.FileNotFoundException

20 / 28

Page 93: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Try/Catch

order matters! class hierarchy matters!

Object

↑Throwable

↑Exception

↑java.io.IOException

↑java.io.FileNotFoundException

20 / 28

Page 94: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Try/Catch

try{

block of code to try}

catch(Exception e){

block of code toexecute if exceptionthrown

}

catch(RuntimeException re){

block of code toexecute if exceptionthrown

}

catch(IOException ioe){

block of code toexecute if exceptionthrown

}

{

block of code to try}

if (Exception is thrown){

block of code toexecute if exceptionthrown

}

else if (RuntimeException){

block of code toexecute if exceptionthrown

}

else if (IOException){

block of code toexecute if exceptionthrown

}

21 / 28

Page 95: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Try/Catch

order matters! class hierarchy matters!

Object

↑Throwable

↑Exception

↑java.io.IOException

↑java.io.FileNotFoundException

22 / 28

Page 96: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Try/Catch/Finaly

· try executes a block of code

· if exception thrown, catch it and handle it

· after try and possibly catch code executes, we finally executesome �nishing code

try{

block of code to try}

catch(Throwable e){

block of codeto execute if exception is caught

}

finally{ // like a funny else

block of code to executeREGARDLESS of what happens above

}

23 / 28

Page 97: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Try/Catch/Finaly

· try executes a block of code

· if exception thrown, catch it and handle it

· after try and possibly catch code executes, we finally executesome �nishing code

try{

block of code to try}

catch(Throwable e){

block of codeto execute if exception is caught

}

finally{ // like a funny else

block of code to executeREGARDLESS of what happens above

}

23 / 28

Page 98: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Try/Catch/Finaly

· try executes a block of code

· if exception thrown, catch it and handle it

· after try and possibly catch code executes, we finally executesome �nishing code

try{

block of code to try}

catch(Throwable e){

block of codeto execute if exception is caught

}

finally{ // like a funny else

block of code to executeREGARDLESS of what happens above

}

23 / 28

Page 99: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Throw

throw is the mechanism to explicitly throw an exception.

· throw new Exception("something bad here...");

creates a new Exception object and throws it

· throw e;

throws an existing exception(e must be a Throwable object )

24 / 28

Page 100: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Throw

throw is the mechanism to explicitly throw an exception.

· throw new Exception("something bad here...");

creates a new Exception object and throws it

· throw e;

throws an existing exception(e must be a Throwable object )

24 / 28

Page 101: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Throw

throw is the mechanism to explicitly throw an exception.

· throw new Exception("something bad here...");

creates a new Exception object and throws it

· throw e;

throws an existing exception(e must be a Throwable object )

24 / 28

Page 102: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Making Your Own Exceptions

you create your own custom exceptions by extending an appropriateThowable class

25 / 28

Page 103: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Making Your Own Exceptions

you create your own custom exceptions by extending an appropriateThowable class

25 / 28

Page 104: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Making Your Own Exceptions

you create your own custom exceptions by extending an appropriateThowable class

public class BadCardRank extends Exception {

public BadCardRank(){

super("Rank of card is invalid");

}

}

26 / 28

Page 105: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

Throwable

the Throwable class is the root of all exceptions in Java

· toString()

· returns short description of this object

· getMessage()

· returns the message as input with the constructor

· printStackTrace()

· prints the stack trace to standard error

27 / 28

Page 106: 200 errors found: Error: /U/TestGQ.java:314: compareTo ...• next need to nd in your code where they occur • JVM might tell you where code crashed (runtime error) ... • println

let's take a break...for 9900 minutes

28 / 28