0% found this document useful (0 votes)
89 views6 pages

Exception Handling and Event

An exception is an object thrown when an error occurs, and try/catch blocks are used to handle exceptions. A try block contains code that might throw an exception, and is followed by one or more catch blocks that handle specific exception types. A finally block always executes regardless of exceptions. There are two types of exceptions: checked exceptions that must be caught, and unchecked exceptions that do not require handling.

Uploaded by

Qiu Le
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views6 pages

Exception Handling and Event

An exception is an object thrown when an error occurs, and try/catch blocks are used to handle exceptions. A try block contains code that might throw an exception, and is followed by one or more catch blocks that handle specific exception types. A finally block always executes regardless of exceptions. There are two types of exceptions: checked exceptions that must be caught, and unchecked exceptions that do not require handling.

Uploaded by

Qiu Le
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Handling Exceptions and Events

Exception
An exception is an object of a specific class.
When an exception occur, an object of a specific class is created

Try / catch / finally block


Is used to handle exceptions within a program

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.

toString (in class Exception)


Returns the detailed message stored in the exception object and the name of the
exception class.

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.

Java’s predefined exceptions


divided into two categories :
 checked exceptions
 unchecked exceptions

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

The method printStackTrace


Is used to determine the order in which the methods were called and where the
exception was handled.

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.

To register a window listener object to a GUI component


use the method addWindowListener.
The window listener object being registered is passed as a parameter to the method
addWindowListener.

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.

tail recursive method


A recursive method in which the last statement executed is the recursive call.

To design a recursive method, you must do the following:


 Understand the problem requirements.
 Determine the limiting conditions.
 Identify the base cases and provide a direct solution to each base case.
 Identify the general case(s) and provide a solution to each general case in
terms of a smaller version of itself.

You might also like