exceptions: checked versus unchecked exceptions

Post on 20-Jan-2016

247 Views

Category:

Documents

3 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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,

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.

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

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.

Some things to remember about Exceptions

• Anything that inherets from RuntimeException is unchecked.

Event Model in Java

Intro to Data Structures in Java

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

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

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/

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.

Cross-Cutting Concerns

• Exceptions• Assertions• Logging

BlackJack Shoe Dealer

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.

Intro to Data Structures in Java

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

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.

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.

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).

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.

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.

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)

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

CORRECT

top related