0% found this document useful (0 votes)
3 views25 pages

Lec 21-22

Uploaded by

ANAS SHEIKH
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)
3 views25 pages

Lec 21-22

Uploaded by

ANAS SHEIKH
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/ 25

CS F213 Object Oriented Programming

Exception in Java

Aritra Mukherjee, Dept. of CSIS


Java Exception
● Definition: An exception is an event, which occurs during the execution of
a program, that disrupts the normal flow of the program's instructions.
● When an error occurs within a method, the method creates an object and
hands it off to the runtime system. The object, called an exception object,
contains information about the error, including its type and the state of
the program when the error occurred. Creating an exception object and
handing it to the runtime system is called throwing an exception.
● After a method throws an exception, the runtime system attempts to find
something to handle it. The set of possible "somethings" to handle the
exception is the ordered list of methods that had been called to get to the
method where the error occurred. The list of methods is known as the call
stack
Java Exception
● The runtime system searches the call stack for a method that contains a
block of code that can handle the exception. This block of code is called an
exception handler. The search begins with the method in which the error
occurred and proceeds through the call stack in the reverse order in which
the methods were called. When an appropriate handler is found, the runtime
system passes the exception to the handler. An exception handler is considered
appropriate if the type of the exception object thrown matches the type that can
be handled by the handler.
● The exception handler chosen is said to catch the exception. If the runtime
system exhaustively searches all the methods on the call stack without finding
an appropriate exception handler, the runtime system (and, consequently, the
program) terminates.
Java Exception
The Three Kinds of Exceptions
● The first kind of exception is the checked exception. These are exceptional
conditions that a well-written application should anticipate and recover from.
Checked exceptions are subject to the Catch or Specify Requirement. All
exceptions are checked exceptions, except for those indicated by Error,
RuntimeException, and their subclasses
● The second kind of exception is the error. These are exceptional conditions
that are external to the application, and that the application usually cannot
anticipate or recover from. Errors are not subject to the Catch or Specify
Requirement. Errors are those exceptions indicated by Error and its
subclasses.
The Three Kinds of Exceptions
● The third kind of exception is the runtime exception or unchecked
exceptions. These are exceptional conditions that are internal to the
application, and that the application usually cannot anticipate or recover from.
These usually indicate programming bugs, such as logic errors or improper
use of an API. Runtime exceptions are not subject to the Catch or Specify
Requirement. Runtime exceptions are those indicated by RuntimeException
and its subclasses.
Exception hierarchy
Exception types
Built-in exceptions
● ArithmeticException: It is thrown when an exceptional condition has
occurred in an arithmetic operation.
● ArrayIndexOutOfBoundsException: It is thrown to indicate that an array
has been accessed with an illegal index. The index is either negative or
greater than or equal to the size of the array.
● ClassNotFoundException: This Exception is raised when we try to access a
class whose definition is not found
● FileNotFoundException: This Exception is raised when a file is not
accessible or does not open.
● IOException: It is thrown when an input-output operation failed or interrupted
Built-in exceptions
● InterruptedException: It is thrown when a thread is waiting, sleeping, or
doing some processing, and it is interrupted.
● NoSuchFieldException: It is thrown when a class does not contain the field
(or variable) specified
● NoSuchMethodException: It is thrown when accessing a method that is not
found.
● NullPointerException: This exception is raised when referring to the
members of a null object. Null represents nothing
● NumberFormatException: This exception is raised when a method could not
convert a string into a numeric format.
Built-in exceptions
● RuntimeException: This represents an exception that occurs during runtime.
● StringIndexOutOfBoundsException: It is thrown by String class methods to
indicate that an index is either negative or greater than the size of the string
● IllegalArgumentException : This exception will throw the error or error
statement when the method receives an argument which is not accurately fit
to the given relation or condition. It comes under the unchecked exception.
● IllegalStateException : This exception will throw an error or error message
when the method is not accessed for the particular operation in the
application. It comes under the unchecked exception.
Usage of exception
Usage of exception
Usage of exception
Control Flow
Control flow in
● try-catch clause OR try-catch-finally clause
○ Case 1: Exception occurs in try block and handled in catch block
○ Case 2: Exception occurs in try-block is not handled in catch block
○ Case 3: Exception doesn’t occur in try-block
● try-finally clause
○ Case 1: Exception occurs in try block
○ Case 2: Exception doesn’t occur in try-block
Custom Exception
Java exceptions cover almost all the general types of exceptions that may occur in
the programming. However, we sometimes need to create custom exceptions.
○ To catch and provide specific treatment to a subset of existing Java
exceptions.
○ Business logic exceptions: These are the exceptions related to business
logic and workflow. It is useful for the application users or the developers
to understand the exact problem.
Custom Exception
Custom Exception
Custom Exception
Chained Exceptions in Java
● Chained Exceptions allows to relate one exception with another exception, i.e
one exception describes cause of another exception.
● Constructors Of Throwable class Which support chained exceptions in java :
○ Throwable(Throwable cause) :- Where cause is the exception that causes
the current exception.
○ Throwable(String msg, Throwable cause) :- Where msg is the exception
message and cause is the exception that causes the current exception.
● Methods Of Throwable class Which support chained exceptions in java :
○ getCause() method :- This method returns actual cause of an exception.
○ initCause(Throwable cause) method :- This method sets the cause for the
calling exception.
Chained Exceptions in Java
Chained Exceptions in Java
Chained Exceptions in Java
Null Pointer Exception… the 😈
These are certain reasons for Null Pointer Exception as mentioned below:

➔ Invoking a method from a null object.


➔ Accessing or modifying a null object’s field.
➔ Taking the length of null, as if it were an array.
➔ Accessing or modifying the slots of null objects, as if it were an array.
➔ Throwing null, as if it were a Throwable value.
➔ When you try to synchronize over a null object.

There are certain methods to handle Null Pointer Exception in Java are mentioned below:
● String comparison with literals
● Keeping a Check on the arguments of a method
● Use of Ternary Operator
The mandatory exception Meme!

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy