Lecture-7-Exceptions
Lecture-7-Exceptions
Eleventh Edition
Chapter 14
Exception Handling and
Event Handling
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
Student's Grade Percent
) (اسم المادة- ) (اسم الدكتور.د
) امتحان الفصل الدراسي (س) لعام2-1(
70 %
60 %
50 %
40 %
30 %
65.32 %
20 %
10 %
15.03 %
0%
8.38 %
Excellent 8.09 %
Very Good
Good 2.60 %
Pass 0.58 %
Weak
Very Weak
Objectives
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
Introduction to Exception Handling
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
Basic Concepts
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
Exception Handling Alternatives
• An exception is raised when its associated event occurs
• A language that does not have exception handling
capabilities can still define, detect, raise, and handle
exceptions (user defined, software detected)
• Alternatives:
– Send an auxiliary parameter or use the return value to
indicate the return status of a subprogram
– Pass a label parameter to all subprograms (error
return is to the passed label)
– Pass an exception handling subprogram to all
subprograms
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
Advantages of Built-in Exception Handling
• Error detection code is tedious to write and it clutters the
program
• Exception handling encourages programmers to consider
many different possible errors
• Exception propagation allows a high level of reuse of
exception handling code
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
Exception Handling Control Flow
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
Exception Handling in C++
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
C++ Exception Handlers
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
The catch Function
• catch is the name of all handlers--it is an overloaded
name, so the formal parameter of each must be unique
• The formal parameter need not have a variable
– It can be simply a type name to distinguish the
handler it is in from others
• The formal parameter can be used to transfer
information to the handler
• The formal parameter can be an ellipsis, in which case it
handles all exceptions not yet handled
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
Throwing Exceptions
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
Unhandled Exceptions (1 of 2)
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
Unhandled Exceptions (2 of 2)
• After a handler completes its execution, control flows to the
first statement after the last handler in the sequence of
handlers of which it is an element
• Other design choices
– All exceptions are user-defined
– Exceptions are neither specified nor declared
– The default handler, unexpected, simply terminates the
program; unexpected can be redefined by the user
– Functions can list the exceptions they may raise
– Without a specification, a function can raise any exception
(the throw clause)
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
Evaluation (1 of 2)
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
Exception Handling in Java
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
Classes of Exceptions (1 of 2)
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
Classes of Exceptions (2 of 2)
▪ Has two predefined subclasses, IOException
and RuntimeException (e.g.,
ArrayIndexOutOfBoundsException and
NullPointerException
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
Java Exception Handlers
• Like those of C++, except every catch requires a
named parameter and all parameters must be
descendants of Throwable
• Syntax of try clause is exactly that of C++
• Exceptions are thrown with throw, as in C++, but often
the throw includes the new operator to create the
object, as in:
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
Binding Exceptions to Handlers (1 of 3)
• Binding an exception to a handler is simpler in Java than
it is in C++
– An exception is bound to the first handler with a
parameter is the same class as the thrown object or
an ancestor of it
• An exception can be handled and rethrown by including
a throw in the handler (a handler could also throw a
different exception)
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
Binding Exceptions to Handlers (2 of 3)
• If no handler is found in the try construct, the search is
continued in the nearest enclosing try construct, etc.
• If no handler is found in the method, the exception is
propagated to the method’s caller
• If no handler is found (all the way to main), the program
is terminated
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
Binding Exceptions to Handlers (3 of 3)
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
Checked and Unchecked Exceptions
• The Java throws clause is quite different from the
throw clause of C++
• Exceptions of class Error and RunTimeException
and all of their descendants are called unchecked
exceptions; all other exceptions are called checked
exceptions
• Checked exceptions that may be thrown by a method
must be either:
– Listed in the throws clause, or
– Handled in the method
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
Other Design Choices
• A method cannot declare more exceptions in its throws
clause than the method it overrides
• A method that calls a method that lists a particular
checked exception in its throws clause has three
alternatives for dealing with that exception:
– Catch and handle the exception
– Catch the exception and throw an exception that is
listed in its own throws clause
– Declare it in its throws clause and do not handle it
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
The finally Clause
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
The finally Clause: Example
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
Assertions (1 of 2)
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
Assertions (2 of 2)
• Two forms
– assert condition;
– assert condition: expression;
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
Evaluation (2 of 2)
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
Exception Handling in Python (1 of 4)
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
Exception Handling in Python (2 of 4)
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
Exception Handling in Python (3 of 4)
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
Exception Handling in Python (4 of 4)
• The assert statement tests its Boolean expression (first
parameter) and sends its second parameter to the
constructor for the exception object to be raised
assert test, data
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
Introduction to Event Handling
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
Java Swing GUI Components
• Text box is an object of class JTextField
• Radio button is an object of class JRadioButton
• Applet’s display is a frame, a multilayered structure
• Content pane is one layer, where applets put output
• GUI components can be placed in a frame
• Layout manager objects are used to control the
placement of components
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
The Java Event Model (1 of 2)
• User interactions with GUI components create events
that can be caught by event handlers, called event
listeners
• An event generator tells a listener of an event by sending
a message
• An interface is used to make event-handling methods
conform to a standard protocol
• A class that implements a listener must implement an
interface for the listener
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
The Java Event Model (2 of 2)
• One class of events is ItemEvent, which is associated
with the event of clicking a checkbox, a radio button, or a
list item
• The ItemListener interface prescribes a method,
itemStateChanged, which is a handler for
ItemEvent events
• The listener is created with addItemListener
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
Summary (1 of 2)
• Ada provides extensive exception-handling facilities with a
comprehensive set of built-in exceptions.
• C++ includes no predefined exceptions
• Exceptions are bound to handlers by connecting the type
of expression in the throw statement to that of the formal
parameter of the catch function
• Java exceptions are similar to C++ exceptions except that
a Java exception must be a descendant of the Throwable
class. Additionally Java includes a finally clause
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
Summary (2 of 2)
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
Copyright
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved