Download presentation
Presentation is loading. Please wait.
Published byMikaela Soward Modified over 9 years ago
1
ELC 310 Day 21
2
© 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 e-mailed Sunday Discussion on Exception Handling
3
Chapter 10 Exceptions
4
© 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
5
© 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
6
© 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
7
© 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
8
© 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
9
© 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
10
© 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
11
© 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
12
© 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
13
© 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
14
© 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
15
© 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
16
© 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
17
© 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
18
© 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
19
ELC 310 Day 23
20
© 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!
21
© 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
22
© 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
23
© 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
24
© 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
25
© 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.