BAB X
Exception Handling
and Event Handling
Handing an exception so when the program is inputted
something that make it crash or not able to continue running because of error,
we can handle it before it happens and the program crashed.
Event Handling is similar to Exception Handling, these two
things differs in the paradigm, one is doing exception the others handle an
event in their own way to run the program further.
asic Concepts
· Many
languages allow programs to trap input/output errors (including EOF)
· An exception
is any unusual event, either erroneous or not, detectable by either hardware or
software, that may require special processing
· The special
processing that may be required after detection of an exception is called
exception handling
· The
exception handling code unit is called an exception handler
· Exception
Handling Alternatives
o An exception is
raised when its associated event occurs
o A language that
does not have exception handling capabilities can still define, detect, raise,
and handle exceptions (user defined, software detected)
o 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
· Advantages
of Built-in Exception Handling
o Error detection
code is tedious to write and it clutters the program
o Exception handling
encourages programmers to consider many different possible errors
·
Exception propagation allows a high level of
reuse of exception handling code
Exception Handling
in C++
o Added to C++ in
1990
o Design is based on
that of CLU, Ada, and ML
o C++ Exception
Handlers
§ Exception Handlers
Form:
try {
--
code that is expected to raise an exception
}
catch
(formal parameter) {
--
handler code
}
...
catch
(formal parameter) {
--
handler code
}
§ 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
o 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
§ Throwing Exceptions
· Exceptions
are all raised explicitly by the statement:
throw [expression];
· The brackets
are metasymbols
· A throw without an operand can only appear in a
handler; when it appears, it simply re-raises the exception, which is then
handled elsewhere
· The type of
the expression disambiguates the intended handler
§ Unhandled
Exceptions
· An unhandled
exception is propagated to the caller of the function in which it is raised
· This
propagation continues to the main function
· If no
handler is found, the default handler is called
§ Continuation
· 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
o All exceptions are
user-defined
o Exceptions are
neither specified nor declared
o The default
handler, unexpected, simply terminates the program; unexpected can be redefined
by the user
o Functions can list
the exceptions they may raise
o Without a
specification, a function can raise any exception (the throw clause)
Introduction to Event Handling
· An event is
a notification that something specific has occurred, such as a mouse click on a
graphical button
· The event
handler is a segment of code that is executed in response to an event
· Java Swing
GUI Components
o Text box is an
object of class JTextField
o Radio button is an
object of class JRadioButton
o Applet’s display
is a frame, a multilayered structure
o Content pane is
one layer, where applets put output
o GUI components can
be placed in a frame
o Layout manager
objects are used to control the placement of components
· The Java
Event Model
o User interactions
with GUI components create events that can be caught by event handlers, called
event listeners
o An event generator
tells a listener of an event by sending a message
o An interface is
used to make event-handling methods conform to a standard protocol
o A class that
implements a listener must implement an interface for the listener
o One class of
events is ItemEvent, which is associated with the event of clicking a checkbox,
a radio button, or a list item
o The ItemListener
interface prescribes a method, itemStateChanged, which is a handler for
ItemEvent events
o The listener is
created with addItemListener
· Event Handling in C#
o Event handling in
C# (and the other .NET languages) is similar to that in Java
o .NET has two
approaches, Windows Forms and Windows Presentation Foundation—we cover only the
former (which is the original approach)
o An application
subclasses the Form predefined class (defined in System.Windows.Forms)
o There is no need
to create a frame or panel in which to place the GUI components
o Label objects are
used to place text in the window
o Radio buttons are
objects of the RadioButton class
o Components are
positioned by assigning a new Point object to the Location property of the
component
private
RadioButton plain = new RadioButton();
plain.Location =
new Point(100, 300);
plain.Text =
″Plain″;
controls.Add(plain);
o All C# event
handlers have the same protocol, the return type is void and the two parameters
are of types object and EventArgs
o An event handler
can have any name
o A radio button is
tested with the Boolean Checked property of the button
private void
rb_CheckedChanged (object o,
EventArgs e)
{
if (plain.Checked)
…
...
}
o To register an
event, a new EventHandler object must be created and added to the predefined delegate for the event
o When a radio
button changes from unchecked to checked, the CheckedChanged event is raised
o The associated
delegate is referenced by the name of the event
o If the handler was
named rb_CheckedChanged, we could register it on the radio button named plain
with:
plain.CheckedChanged +=
new EventHandler
(rb_CheckedChanged);
Tidak ada komentar:
Posting Komentar