0% found this document useful (0 votes)
44 views

Exception Handling

Uploaded by

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

Exception Handling

Uploaded by

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

EXCEPTION

HANDLING
6.53

CIT103 Computer Programming


WHAT IS EXCEPTION HANDLING ?
Exception handling is a programming concept in which errors
and other exceptional events are handled in a structured and
controlled manner. In Java, exceptions are objects that represent
errors or other exceptional events that occur during the execution
of a program. These events may include errors such as division
by zero or file not found, or other exceptional conditions such as
network errors or database connection failures.

11/04//23 Exception Handling 2


WHAT IS EXCEPTION HANDLING ?

Exception handling involves anticipating potential errors and using


code to handle them in a way that allows the program to continue
executing or gracefully terminate. This is typically done using try-
catch blocks, where code that may throw an exception is enclosed in
a try block and code to handle the exception is enclosed in a catch
block. If an exception is thrown, the catch block is executed, and the
program can take appropriate action to handle the error.

11/04/23 Error Handling 3


WHY USE EXCEPTION HANDLING
In Java, error handling is an important concept that involves the identification and
resolution of errors or exceptions that occur during the execution of a program. Java
provides a robust framework for handling errors through its exception handling
mechanism, which involves the use of predefined classes such as Throwable, Exception,
and Error.

411/4/23 Exception Handling 4


WHY USE EXCEPTION HANDLING
By using exception handling, programs can be made more robust and reliable, as errors
and other exceptional events can be detected and handled in a controlled manner. This
can help prevent crashes or other unexpected behavior, and can make it easier to
diagnose and fix issues in a program.

411/4/23 Exception Handling 5


ERROR HANDLING CLASSES
Throwable: This is the root class of the Java exception class hierarchy. All exceptions
and errors in Java inherit from this class. The two direct subclasses of Throwable are
Error and Exception.
Error: This class represents serious problems that are beyond the control of the
program, such as virtual machine errors, out of memory errors, etc. These errors usually
indicate problems that cannot be recovered from, and so the program terminates.

11/4/23 Error Handling 6


ERROR HANDLING CLASSES
Exception: This class represents less serious problems that can be recovered from, such
as input/output errors, arithmetic errors, etc. Exceptions can be handled through try-
catch blocks and other mechanisms.
RuntimeException: This is a subclass of Exception that represents exceptions that
occur due to programming errors, such as null pointer exceptions, array index out of
bounds exceptions, etc. Unlike checked exceptions, which must be caught or declared,
runtime exceptions are not required to be caught or declared.

11/4/23 Error Handling 7


ERROR HANDLING CLASSES
ArithmeticException: This is a subclass of RuntimeException that represents
exceptions that occur due to arithmetic errors, such as divide-by-zero errors.

NullPointerException: This is a subclass of RuntimeException that represents


exceptions that occur when a program tries to access a null object reference.

ArrayIndexOutOfBoundsException: This is a subclass of RuntimeException that


represents exceptions that occur when a program tries to access an array element that is
outside the bounds of the array.

11/4/23 Error Handling 8


ERROR HANDLING CLASSES

In addition to these classes, Java provides a wide range of predefined exceptions that can
be used to handle specific types of errors. It is also possible to define custom exception
classes to handle program-specific errors.

11/4/23 Error Handling 9


FINALLY VS TRY-CATCH
Java also provides a finally block, which
is executed regardless of whether an
exception is thrown or not. This can be
used to perform cleanup tasks, such as
closing files or releasing resources, that
must be done regardless of whether an
exception occurs.

11/4/2023 Pitch deck title 10


TRY-CATCH JAVA EXAMPLE

11/4/23 Error Handling 11


EXAMPLE OF WHEN EXCEPTIONS OCCUR
IOEXCEPTION

This code reads the contents of a file named "file.txt". If an IOException occurs (e.g., if
the file does not exist or cannot be read), the catch block will handle the exception.

11/4/23 Error Handling 12


EXAMPLE OF WHEN EXCEPTIONS OCCUR
RUNTIMEEXCEPTION && NULLPOINTEREXCEPTION

A run time error occurs when the program is being executed length() method is called
on a null reference str. The length() method is a method of the String class that returns
the length of the string, but since str is null, there is no object on which to call the
method, and the program will crash with a NullPointerException.

11/4/23 Error Handling 13


EXAMPLE OF WHEN EXCEPTIONS OCCUR
ARITHMETICEXCEPTION

This code tries to get the length of a null array, resulting in a NullPointerException

11/4/23 Error Handling 14


EXAMPLE OF WHEN EXCEPTIONS OCCUR
ARITHMETICEXCEPTION

This code tries to access an array element that is outside the bounds of the array, resulting
in an ArrayIndexOutOfBoundsException.

11/4/23 Error Handling 15


IMPLEMENTING TRY-CATCH BLOCK
IOEXCEPTION

11/04//23 Exception Handling 16


IMPLEMENTING TRY-CATCH BLOCK
NULLPOINTEREXCEPTION

11/04//23 Exception Handling 17


IMPLEMENTING TRY-CATCH BLOCK
NULLPOINTEREXCEPTION

This code tries to get the length of a null string. If a NullPointerException


occurs, the catch block will handle the exception and print an error message. The
finally block will always be executed, regardless of whether an exception is
thrown or not.

11/04//23 Exception Handling 18


CUSTOM EXCEPTIONS
WHAT ARE CUSTOM EXCEPTIONS ?

Custom exceptions, also known as user-defined exceptions, are


exceptions that are defined by the programmer to represent specific
error conditions that may occur in a program. In Java, custom
exceptions are created by defining a new class that extends the
Exception or RuntimeException class.

11/04/23 Error Handling 20


WHY USE CUSTOM EXCEPTIONS ?

Custom exceptions can be useful when a program needs to handle


specific error conditions that are not covered by the built-in
exception classes, or when a program needs to provide more
detailed information about an error than can be conveyed by a
standard exception.

11/04/23 Error Handling 21


UTILIZING A CUSTOM EXCEPTION ?

For example, imagine a program that processes customer orders. If


the program encounters an error while processing an order, it could
throw a RuntimeException with a generic error message. However,
if the program defines a custom exception class
OrderProcessingException that includes specific details about the
error, such as the customer name and order number, it can provide
more detailed information that can be used to diagnose and fix the
error.
11/04/23 Error Handling 22
CUSTOM EXCEPTION CODE

11/04//23 Exception Handling 23


This custom exception class InvalidOrderException extends the Exception class and
includes an additional field orderNumber to store the order number associated with the
exception. The class also defines a constructor that takes both an order number and an
error message, and a getter method to retrieve the order number. This custom exception
class can be used in a program to represent errors related to invalid orders, and can
provide additional information about the error to help diagnose and fix the issue.

411/4/23 Exception Handling 24


CUSTOM EXCEPTION TRY-CATCH BLOCK

11/04//23 Exception Handling 25


In the code above , the processOrder method processes an order with the specified
orderNumber. The try block contains some code that processes the order, and checks if
the order number is valid. If the order number is less than 100, the code throws a new
InvalidOrderException with the order number and an error message.

411/4/23 Exception Handling 26


The catch block catches the InvalidOrderException, which is the custom exception class
we defined earlier. In the catch block, we print an error message that includes the order
number and the message from the exception. We can also handle the exception in some
other way, such as logging the error or notifying the user.

By using a custom exception class like InvalidOrderException, we can provide more


specific and detailed error messages to help diagnose and fix problems in our code or
user crafted errors.

411/4/23 Exception Handling 27

You might also like