ELC 310 Day 21. © 2004 Pearson Addison-Wesley. All rights reserved10-2 Agenda Questions? Capstone Proposals Overdue  4 Submitted  3 Accepted, 1 in negotiations.

Slides:



Advertisements
Similar presentations
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Advertisements

CS102--Object Oriented Programming
An Introduction to Java Programming and Object- Oriented Application Development Chapter 8 Exceptions and Assertions.
COMP 121 Week 5: Exceptions and Exception Handling.
© 2004 Pearson Addison-Wesley. All rights reserved10-1 Chapter 10 : Exceptions Intermediate Java Programming Summer 2007.
1 Chapter 10: Exceptions and I/O Streams Original Slides by John Lewis and William Loftus Modified significantly by Bob Roggio.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Exceptions Briana B. Morrison CSE 1302C Spring 2010.
File I/O and Exceptions File I/O Exceptions Throwing Exceptions Try statement and catch / finally clauses Checked and unchecked exceptions Throws clause.
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.
Slides prepared by Rose Williams, Binghamton University Chapter 9 Exception Handling.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Exceptions. Errors and Exceptions An error is a bug in your program –dividing by zero –going outside the bounds of an array –trying to use a null reference.
Chapter 8: Exceptions and I/O Streams Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,
Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter Day 25. © 2007 Pearson Addison-Wesley. All rights reserved Agenda Day 25 Problem set 5 Posted (Last one)  Due Dec 8 Capstones Schedule  3rd.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
Chapter 8: Exceptions and I/O Streams Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
Input/Ouput and Exception Handling. 2 Exceptions  An exception is an object that describes an unusual or erroneous situation  Exceptions are thrown.
© 2004 Pearson Addison-Wesley. All rights reserved10-1 Outline Polymorphic References Polymorphism via Inheritance Polymorphism via Interfaces Sorting.
Java Software Solutions Foundations of Program Design Sixth Edition
Preventing and Correcting Errors
Object Oriented Programming
CIS 270—Application Development II Chapter 13—Exception Handling.
Chapter 10 Exceptions and File I/O. © 2004 Pearson Addison-Wesley. All rights reserved10-2 Exceptions Exception handling is an important aspect of object-oriented.
10-1 Exceptions An exception is an object that describes an unusual or erroneous situation Exceptions are thrown by a program, and may be caught and handled.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Graphical User Interfaces Revisited Material from Chapters
Java Programming: Guided Learning with Early Objects
Java Software Solutions Lewis and Loftus Chapter 14 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Advanced Flow of Control --
Chapter 10 Exceptions. Chapter Scope The purpose of exceptions Exception messages The call stack trace The try-catch statement Exception propagation The.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Exceptions Chapter 10 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
Exceptions in Java. Exceptions An exception is an object describing an unusual or erroneous situation Exceptions are thrown by a program, and may be caught.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
Data Structures Using Java1 Chapter 2 Inheritance and Exception Handling.
Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about.
© 2004 Pearson Addison-Wesley. All rights reserved April 24, 2006 Exceptions (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING 2006.
1 Chapter 10: Exceptions and I/O Streams Original Slides by John Lewis and William Loftus Modified significantly by Bob Roggio, July 2007.
Exceptions in Java. What is an exception? An exception is an error condition that changes the normal flow of control in a program Exceptions in Java separates.
Chapter 10 Exceptions 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
© 2004 Pearson Addison-Wesley. All rights reserved December 5, 2007 I/O Exceptions & Working with Files ComS 207: Programming I (in Java) Iowa State University,
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
10 Exceptions Software Solutions Lewis & Loftus java 5TH EDITION
Chapter 10 Exceptions.
Exception Handling Chapter 9.
04/14/14 Exceptions.
Exceptions Exception handling is an important aspect of object-oriented design Chapter 10 focuses on the purpose of exceptions exception messages the.
Exceptions Problems in a Java program may cause exceptions or errors representing unusual or invalid processing. An exception is an object that defines.
Abdulmotaleb El Saddik University of Ottawa
Exception Handling Chapter 9 Edited by JJ.
Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013
Exception Handling Imran Rashid CTO at ManiWeber Technologies.
Chapter 10 Exceptions.
Exceptions (part 2) December 3, 2007 ComS 207: Programming I (in Java)
I/O Exceptions & Working with Files
Chapter 11 Exceptions Java Software Solutions
CMSC 202 Exceptions 2nd Lecture.
Presentation transcript:

ELC 310 Day 21

© 2004 Pearson Addison-Wesley. All rights reserved10-2 Agenda Questions? Capstone Proposals Overdue  4 Submitted  3 Accepted, 1 in negotiations  Proposal should be in correct format (see guidelines in WebCT) Capstone progress reports overdue Problem set 4 partially corrected  1 A, 1 B and 2 C’s  Not doing some of the problems really hurts your score Problem set 5 Parts A & B  Each worth 50 Points  Due November 22 and Dec 2 respectively Exam #3  November 22  Chap 7, 8 & 9 25 M/C  Available 7AM-7PM, Passwords will be ed Sunday Discussion on Exception Handling

Chapter 10 Exceptions

© 2004 Pearson Addison-Wesley. All rights reserved10-4 Exceptions Exception handling is an important aspect of object-oriented design Chapter 10 focuses on:  the purpose of exceptions  exception messages  the try-catch statement  propagating exceptions  the exception class hierarchy  GUI mnemonics and tool tips  more GUI components and containers

© 2004 Pearson Addison-Wesley. All rights reserved10-5 Outline Exception Handling The try-catch Statement Exception Propagation Exception Classes I/O Exceptions Tool Tips and Mnemonics Combo Boxes Scroll Panes and Split Panes

© 2004 Pearson Addison-Wesley. All rights reserved10-6 Exception Handling An exception is an object that defines an unusual or erroneous situation  Divide by zero  Index out of bounds  Can’t find a file  Security violation Exceptions are “thrown” and “caught”  Catching an exception allows you to recover from the problem An error is similar to an exceptions except that errors are generally unrecoverable There are 3 things you can do with an exception  Ignore it  Handle it where it happens  Handle at some other point in the program

© 2004 Pearson Addison-Wesley. All rights reserved10-7 Uncaught (ignored) Exceptions Uncaught exceptions cause program termination  Produces a message What exception occured Where the exception occurred Example  Zero.java Zero.java  Exception in thread "main" java.lang.ArithmeticException: / by zero ÏÏ§Ï at Zero.main(Zero.java:17)  Message includes Information about the exception –Method Exception.getmessage() Call stack trace –Method, file and line number –Method Exception.printStackTrace() Since the exceptions was not “caught”, the program terminated and the message was passed to the 0/S as output

© 2004 Pearson Addison-Wesley. All rights reserved10-8 Outline Exception Handling The try-catch Statement Exception Propagation Exception Classes I/O Exceptions Tool Tips and Mnemonics Combo Boxes Scroll Panes and Split Panes

© 2004 Pearson Addison-Wesley. All rights reserved10-9 try-catch Try-catch allows the program to catch and handle an exception when it is thrown  Fix the problem  try { block of code} catch (exception) { code to handle the exception} If the code in the try block does not produce an exception the catch block is skipped try Block catch handler No exception exception

© 2004 Pearson Addison-Wesley. All rights reserved10-10 Try-Catch Examples  ProductCodes.java ProductCodes.java  ProductCodes1.java ProductCodes1.java Since the exceptions are caught and handled, the processing of the program does not terminate till the user explicitly terminates the program The “finally” clause  Optional after a try-catch try Block catchhandler No exception exception Finally Block

© 2004 Pearson Addison-Wesley. All rights reserved10-11 Outline Exception Handling The try-catch Statement Exception Propagation Exception Classes I/O Exceptions Tool Tips and Mnemonics Combo Boxes Scroll Panes and Split Panes

© 2004 Pearson Addison-Wesley. All rights reserved10-12 Exception Propagation If an exception is not caught and handled, control moves to the method that called the method that threw the exception. You can handle the exception at the higher level  Called “propagating the exception”  The exception will propagate ‘up’ the stack till It is handled Or, it forces the program to terminate Example  Propagation.java Propagation.java  ExceptionScope.java ExceptionScope.java

© 2004 Pearson Addison-Wesley. All rights reserved10-13 Exception Propagation The exceptions moves up the stack of calling methods doIt helpMe helpMe(); obj.doIt(); main Exception thrown Exception thrown Exception Thrown Exception occurred

© 2004 Pearson Addison-Wesley. All rights reserved10-14 Outline Exception Handling The try-catch Statement Exception Propagation Exception Classes I/O Exceptions Tool Tips and Mnemonics Combo Boxes Scroll Panes and Split Panes

© 2004 Pearson Addison-Wesley. All rights reserved10-15 Exception Class Hierarchy Throwable class is parent of error and exception class You can define your own exceptions  Inheriting from an Exception class

© 2004 Pearson Addison-Wesley. All rights reserved10-16 Creating your own exception Example  CreatingExceptions.java CreatingExceptions.java  OutOfRangeException.java OutOfRangeException.java Checked and Unchecked Exceptions  Checked exceptions must be caught or explicitly thrown by listing in the “throws” clause  An unchecked exception requires no throws clause RunTimeException class (and it descendants) are the only unchecked exceptions in JAVA

© 2004 Pearson Addison-Wesley. All rights reserved10-17 Outline Exception Handling The try-catch Statement Exception Propagation Exception Classes I/O Exceptions Tool Tips and Mnemonics Combo Boxes Scroll Panes and Split Panes

© 2004 Pearson Addison-Wesley. All rights reserved10-18 I/O Exceptions I/O is JAVA is treated as a Stream  Ordered sequence of bytes from a source to a destination  Read from input streams  Write to output streams Three standard I/O streams  System.in > Input (keyboard)  System.out > Output (monitor)  System.err >Output for error messages (monitor) I/O streams throws IOexception  Checked exception Example  TestData.java test.dat TestData.java test.dat  TestData1.java test2.dat TestData1.javatest2.dat

ELC 310 Day 23

© 2004 Pearson Addison-Wesley. All rights reserved10-20 Agenda Questions? Capstone progress due Student Evaluation Problem set 5 Parts A Corrected  Good results Problem set 5 Part B  DUE Dec 2 Exam #3 Corrected  Poor results  1 A, 2 C’s and 1 D Discussion on  Tool tips and Mnemonics  Advanced GUI components Tool Tips and Mnemonics Combo Boxes Scroll Panes and Split Panes Next is Recursion, generally a difficult subject for CS students  Read ahead!

© 2004 Pearson Addison-Wesley. All rights reserved10-21 Tool tips and Mnemonics Tool tip  Short Line of text that appears when ever the cursor is in top of the Swing component  Works with all Swing Components (J*)  *.setTooTipText (“A message”); Mnemonic  Hot key (ALT-”a key”)  *.setMnemonic (‘C’);  Underlines letter Disabling components  *.setEnabled( false);  Grays out component Examples  Lightbulb.java Lightbulb.java  LightBulbPanel.java LightBulbPanel.java  LightBulbControls.java LightBulbControls.java

© 2004 Pearson Addison-Wesley. All rights reserved10-22 Combo boxes Combo Boxes create “Drop Down” menus Defined in JComboBox Class Two ways to add items to the drop down  Using the constructor  *.addItem(“the item”); Can also use ImageIcon Can be editable or uneditable  Default is uneditable New object  URL Url(“protocol”, “host”, ‘file” New interface  AudioClip *.loop(); *.play(); *.stop(); Example  JukeBox.java JukeBox.java  JukeBoxControls.Java JukeBoxControls.Java

© 2004 Pearson Addison-Wesley. All rights reserved10-23 Scroll Panes For Images and Information that is too large to fit in the assigned area Provides Scroll bars to navigate through the larger information or image Defined in JScrollPane class Example  TransmitMap.java TransmitMap.java

© 2004 Pearson Addison-Wesley. All rights reserved10-24 Split panes Panel that displays twp components separated by a movable divider bar  Horizontal Left, right  Vertical Top, bottom Defined in JSplitPane class Example  ListPanel.java ListPanel.java  PickImage.java PickImage.java

© 2004 Pearson Addison-Wesley. All rights reserved10-25 Summary Chapter 10 has focused on: the purpose of exceptions exception messages the try-catch statement propagating exceptions the exception class hierarchy GUI mnemonics and tool tips more GUI components and containers