Presentation is loading. Please wait.

Presentation is loading. Please wait.

Exceptions: Checked versus Unchecked Exceptions.

Similar presentations


Presentation on theme: "Exceptions: Checked versus Unchecked Exceptions."— Presentation transcript:

1 Exceptions: Checked versus Unchecked Exceptions

2

3

4 Probable Source Within your control Probable Source Checked ErrorNOSystemNO RuntimeExce ption YESProgrammerNO All other exceptions NOUserYES Example of Error: System-out-of-memory Error Examples of RuntimeException: ClassCastException, NumberFormatException. Example of checked Exceptions: EOFExcception,

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

6 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

7

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

9 Some things to remember about Exceptions Anything that inherets from RuntimeException is unchecked.

10 Event Model in Java

11

12 Intro to Data Structures in Java Single Lined List Stack Binary Tree others...

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

14 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/

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

16 Cross-Cutting Concerns Exceptions Assertions Logging BlackJackShoeDealer

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

18

19 Intro to Data Structures in Java Single Lined List Stack Binary Tree others...

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

21

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

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

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

25 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. Each DictEntry has two strings. Build a GUI using WindowBuilder and Swing.

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

27 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

28 CORRECT


Download ppt "Exceptions: Checked versus Unchecked Exceptions."

Similar presentations


Ads by Google