Exception Handling and Event
Exception Handling and Event
Exception
An exception is an object of a specific class.
When an exception occur, an object of a specific class is created
Try block
Statements that may generate an exception are placed in a try block.
The try block also contain statements that should not be executed if an
exception occurs.
A try block is followed by zero or more catch block.
Catch block
Specifies the type of exception it can catch and contain an exception handler.
Last catch block may or may not be followed by a finally block.
Can catch all exception of a specific type / all type of exception.
Heading of a catch block specifies the type of exception it handles.
#Typically, a catch block does one of the following:
Completely handles the exception.
Partially processes the exception.
o In this case, the catch block either rethrows the same exception or
throws another exception for the calling environment to handle the
exception.
Finally block
Finally block always executes, whether an exception is occur or not, except
when the program exit early from a try block by using the method
system.exit.
If a try block is not followed by a catch block, then it must have finally
block.
Class Throwable
class Throwable is derived from the class Object, is the superclass of the
class Exception.
The methods getMessage, printStackTrace, and toString of the class
Throwable are public.
so these methods are inherited by the subclasses of the class Throwable.
getMessage
Returns the string containing the detailed message stored in the exception object.
Class Exception
Its subclasses are designed to catch exceptions that should be caught and
processed during program execution.
Is the superclass of the classes designed to handle exceptions.
Is contained in the package java.lang.
Generally placed in the package that contains the methods that throw these
exceptions.
Class InputMismatchException
Is contained in the package java.util.
The classes deal with number format exceptions and arithmetic exceptions,
such as division by zero, are contained in the package java.lang.
Checked Exception
Any exception that can be recognized by the compiler
Unchecked exceptions
Exceptions that are not recognized by the compiler.
throw exceptionReference
The general syntax to rethrow an exception caught by a catch block
throw exceptionReference
Rethrow an exception caught by a catch block.
ExceptionClassName(messageString)
throw your own exception object
Action events
handled by appropriately implementing the interface ActionListener.
Window events
handled by appropriately implementing the interface WindowListener.
class WindowAdapter
Implements the interface WindowListener by providing empty bodies to the
methods.
Mouse events
handled by appropriately implementing the interface MouseListener.
class MouseAdapter
Implements the interface MouseListener by providing empty bodies to the
methods.
To register a mouse listener object to a GUI component
use the method addMouseListener.
The mouse listener object being registered is passed as a parameter to the method
addMouseListener.
Key events
Handled by appropriately implementing the interface KeyListener.
Recursion.
Definition: defines the problem in terms of smaller versions of itself.
The process of solving a problem by reducing it to smaller versions of itself
(Recursive algorithm)
Every recursive definition has one or more base cases. (base case stops the
recursion)
A method is recursive if it calls itself
Recursive algorithms are implemented using recursive methods
general solution breaks a problem into smaller versions of itself.
general case must eventually be reduced to a base case.
Indirectly recursive
A method that calls another method and eventually results in the original method
call.