elc 310 day 21. © 2004 pearson addison-wesley. all rights reserved10-2 agenda questions? capstone...
Post on 16-Dec-2015
215 Views
Preview:
TRANSCRIPT
ELC 310
Day 21
© 2004 Pearson Addison-Wesley. All rights reserved 10-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 e-mailed Sunday
• Discussion on Exception Handling
Chapter 10
Exceptions
© 2004 Pearson Addison-Wesley. All rights reserved 10-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 reserved 10-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 reserved 10-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 reserved 10-7
Uncaught (ignored) Exceptions• Uncaught exceptions cause program termination
Produces a message• What exception occured• Where the exception occurred
• Example 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 reserved 10-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 reserved 10-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 reserved 10-10
Try-Catch
• Examples ProductCodes.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
catch handler
No exception
exception
FinallyBlock
© 2004 Pearson Addison-Wesley. All rights reserved 10-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 reserved 10-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 ExceptionScope.java
© 2004 Pearson Addison-Wesley. All rights reserved 10-13
Exception Propagation
• The exceptions moves up the stack of calling methods
doIt
helpMe
helpMe();
obj.doIt();
main
ExceptionthrownException
thrownExceptionThrown
Exceptionoccurred
© 2004 Pearson Addison-Wesley. All rights reserved 10-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 reserved 10-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 reserved 10-16
Creating your own exception
• Example CreatingExceptions.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 reserved 10-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 reserved 10-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 TestData1.java test2.dat
ELC 310
Day 23
© 2004 Pearson Addison-Wesley. All rights reserved 10-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 reserved 10-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 LightBulbPanel.java LightBulbControls.java
© 2004 Pearson Addison-Wesley. All rights reserved 10-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 JukeBoxControls.Java
© 2004 Pearson Addison-Wesley. All rights reserved 10-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
© 2004 Pearson Addison-Wesley. All rights reserved 10-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 PickImage.java
© 2004 Pearson Addison-Wesley. All rights reserved 10-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
top related