Exceptions: Checked versus Unchecked Exceptions.

28
Exceptions: Checked versus Unchecked Exceptions

Transcript of Exceptions: Checked versus Unchecked Exceptions.

Page 1: Exceptions: Checked versus Unchecked Exceptions.

Exceptions:

Checked versus Unchecked Exceptions

Page 2: Exceptions: Checked versus Unchecked Exceptions.
Page 3: Exceptions: Checked versus Unchecked Exceptions.
Page 4: Exceptions: Checked versus Unchecked Exceptions.

Probable Source

Within your control

Probable Source

Checked

Error NO System NORuntimeException

YES Programmer NO

All other exceptions

NO User YES

Example of Error: System-out-of-memory ErrorExamples of RuntimeException: ClassCastException, NumberFormatException.Example of checked Exceptions: EOFExcception,

Page 5: Exceptions: Checked versus Unchecked Exceptions.

Situations which cause Exceptions (or Errors) to be thrown

• An internal Error occurs in the VM (OutOfMemoryError)

• An unchecked Exception is generated and thrown (ArrayIndexOutOfBounds)

• A checked Exception is thrown (EOFException)• You purposefully throw an Exception.

Page 6: Exceptions: Checked versus Unchecked Exceptions.

Exception sequence

• Once an exception is generated, the execution sequence immediately terminates until the exception is caught (or propagates up the call stack to the VM). In practice that means that any code following your statement which threatens to throw an exception will NOT be executed if an exception occurs. No return value!

• An exception is thrown up the call stack

Page 7: Exceptions: Checked versus Unchecked Exceptions.
Page 8: Exceptions: Checked versus Unchecked Exceptions.

Some Java naming wierdness in the Exception Handling API

• Despite having the 'able' suffix, Throwable is a class, not an interface.

• Errors are considered part of the "Exception Handling" API

• All Exceptions and Errors occur at Runtime, so RuntimeException is a useless name.

Page 9: Exceptions: Checked versus Unchecked Exceptions.

Some things to remember about Exceptions

• Anything that inherets from RuntimeException is unchecked.

Page 10: Exceptions: Checked versus Unchecked Exceptions.

Event Model in Java

Page 11: Exceptions: Checked versus Unchecked Exceptions.
Page 12: Exceptions: Checked versus Unchecked Exceptions.

Intro to Data Structures in Java

• Single Lined List• Stack• Binary Tree• others...

Page 13: Exceptions: Checked versus Unchecked Exceptions.

"If I can't picture it, I can't understand it."

Page 14: Exceptions: Checked versus Unchecked Exceptions.

Source Code Online• Please avail yourselves of the many excellent

sources for source code, both full applications and snippets.

• http://stackoverflow.com/ (best for snippets)• http://www.planet-source-code.com/ (best

for full apps)• http://www.freewarejava.com/• http://www.javagalaxy.com/• http://www.java2s.com• http://www.javadb.com/

Page 15: Exceptions: Checked versus Unchecked Exceptions.

Guidelines for using found source code• If you know the URL where you got it, cite like

so:– //http://stackoverflow.com/questions/26305/

how-can-i-play-sound-in-java– At minimum, cite the domain:– //method found on stackoverflow.com

• Don't copy an entire application; that is plagiarism.

• Use found source code within reason.

Page 16: Exceptions: Checked versus Unchecked Exceptions.

Cross-Cutting Concerns

• Exceptions• Assertions• Logging

BlackJack Shoe Dealer

Page 17: Exceptions: Checked versus Unchecked Exceptions.

Inner and Anonymous Classes

• Though you are captive in the OO paradigm, you can use inner classes and anonymous classes to get around this constraint and write procedural-like code.

• Often times, no one but the enclosing class cares about an object. In this case, you may consider using inner or anonymous classes.

Page 18: Exceptions: Checked versus Unchecked Exceptions.
Page 19: Exceptions: Checked versus Unchecked Exceptions.

Intro to Data Structures in Java

• Single Lined List• Stack• Binary Tree• others...

Page 20: Exceptions: Checked versus Unchecked Exceptions.

WindowsBuilderPro by Google

• b,y,c,n,s,l,d,f,str; get rid of these in Window || Preferences || Java || Code Style || Fields. Otherwise, they will hose you in WB.

Page 21: Exceptions: Checked versus Unchecked Exceptions.
Page 22: Exceptions: Checked versus Unchecked Exceptions.

Constructors• Constructors are optional. If your class is just a static

driver; i.e. it just has a static main method, then no need to instantiate it; and no need for constructor.

• If your class is a "java bean" -- a class that models a real world object (noun) in your system, then it should have a constructor. House, Car, Person, etc.

• If you don't provide a constructor, a no-args constructor is provided automatically.

• You can, and often want, more than one constructor with different signatures.

• You call a constrcutor with the 'new' keyword.

Page 23: Exceptions: Checked versus Unchecked Exceptions.

Model a system• Object-oriented programming is about

modeling a system. • Write the problem out as requirements -- this

will essentially be a math world problem (your favorite kind to solve!)

• Your computer objects map directly to real-world objects (nouns).

• Your methods map directly to real-world actions (verbs).

Page 24: Exceptions: Checked versus Unchecked Exceptions.

Write a very simple application for a contact manager. The the sake of simplicity, each contact will have a name and a phone number only. The user should be able to create new contacts and diplay all contacts.

Build a GUI using WindowBuilder and Swing.

Page 25: Exceptions: Checked versus Unchecked Exceptions.

Write a very simple application for computer dictionary. The the sake of simplicity, each entry has a word and a definition. Store the pair in an ArrayList<DictEntry>. Each DictEntry has two strings. Build a GUI using WindowBuilder and Swing.

Page 26: Exceptions: Checked versus Unchecked Exceptions.

Describe the system:

The game of BlackJack; a single player plays against the house for money. His bet is consistently $100.00 and he starts with 1,000.00. There is a shoe of six 52-card decks which is reshuffled when the shoe is half used. If the player wins the hand, his gets his bet back plus the amount of the bet. If he loses, he loses the money, and if he gets blackjack, he get's his bet * 1.5.

The player plays against a dealer who must follow the following strict rules; aces are worth 11points only, and the dealer must hit on 16 or below. The player however, is allowed to hit or hold on any hand-value. Furthermore, aces are worth either 1 or 11, whichever is more advantageous.

An initial two hands are dealt on seperate sides of a table consisting of two cards apiece. The dealer's hand displays only one card up. The player has the option to hit, hold, split, double-down, buy insurance, etc. For this initial version of the game, we'll consider only hit, hold, and deal.

blue are nouns (objects or fields)red are verbs (methods)

Page 27: Exceptions: Checked versus Unchecked Exceptions.

Interfaces

• A class implements an interface rather than extends it. Any class that implements the interface must override all the interface methods with it's own methods.

• Interface names often end with "able" to imply that they add to the capabilty of the class.

• An interface is a contract; it defines the methods

Page 28: Exceptions: Checked versus Unchecked Exceptions.

CORRECT