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

Java Hamza 8

Uploaded by

selanawalahamza
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Java Hamza 8

Uploaded by

selanawalahamza
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

DEPARTMENT OF INFORMATION TECHNOLOGY

Course: Java Programming (ITXS37)


B. Tech. (Information Technology)— Semester Ill
Academic Year: 2024-25 (Odd Semester)

Experiment No: 8

Aim: To study the concept of Exception Handling

Problem Statement: Java Program to Create Account with 1000 Rs Minimum


Balance, Deposit Amount, Withdraw Amount and Also Throws
LessBalanceException. It has a Class Called LessBalanceException Which returns
the Statement that Says WithDraw Amount(_Rs) is Not Valid. It has a Class Which
Creates 2 Accounts, Both Account Deposite Money and One Account Tries to
WithDraw more Money Which Generates a LessBalanceException Take Appropriate
Action for the Same.

Objective :
To recognize usage of Exception Handling in various applications

Theory:
Exception Handling in Java is a mechanism that deals with runtime errors, ensuring
the normal flow of the program isn't disrupted by unexpected errors or exceptions.
An exception is an event that occurs during the execution of a program that disrupts
its normal flow.
Key Concepts:
1. Exception:
o An exception is an object that encapsulates an error event that occurs
during the execution of a program. In Java, exceptions are objects that
derive from the java.lang.Exception class.
o Example: Trying to divide a number by zero (ArithmeticException) or
accessing an invalid array index (ArrayIndexOutOfBoundsException).
2. Exception Hierarchy:
o Checked exceptions: These are exceptions checked at compile time.
They must be either handled using a try-catch block or declared in the
method signature with a throws clause. Examples include IOException,
SQLException.
o Unchecked exceptions (Runtime Exceptions): These occur during the
execution of a program. Unchecked exceptions do not need to be
declared or handled explicitly. They include errors like
NullPointerException, ArrayIndexOutOfBoundsException.
o Errors: These represent serious problems that are generally not
recoverable, such as OutOfMemoryError.

3. Key Exception Handling Keywords:

 try: The block of code that is monitored for exceptions. If an exception occurs,
the control is transferred to the corresponding catch block.

 catch: This block catches and handles the exception thrown in the try block.
You can have multiple catch blocks to handle different types of exceptions.

 finally: The finally block always executes after the try and catch blocks,
regardless of whether an exception occurred or not. It's typically used for
resource cleanup, like closing file streams or database connections.

 throw: The throw keyword is used to explicitly throw an exception from a


method or block of code. This is useful for creating user-defined exceptions or
when certain conditions in a program require an exception to be thrown.

 throws: This is used in the method signature to indicate that the method may
throw certain exceptions, which must be handled by the calling method.
4. Commonly Thrown Exceptions:

 ArithmeticException: Thrown when an illegal arithmetic operation occurs (e.g.,


division by zero).

 NullPointerException: Thrown when a null reference is accessed.

 ArrayIndexOutOfBoundsException: Thrown when an array is accessed with


an illegal index.

 ClassNotFoundException: Thrown when trying to load a class that isn't found.

 IOException: Thrown during input/output operations like reading a file.

 SQLException: Thrown when an SQL operation fails.


5. Custom Exceptions: Java also allows developers to create their own exceptions.
These custom exceptions can provide more meaningful error handling in specific
application contexts.
java
Benefits of Exception Handling:

 Improved Error Management: Rather than crashing the program, exceptions


allow it to recover gracefully by catching errors and responding accordingly.

 Separation of Error-Handling Code: With exception handling, the logic of error


handling is separated from the regular program logic, making code easier to
understand.

 Prevention of Program Crashes: Exception handling can prevent programs


from terminating unexpectedly due to unhandled exceptions.
Example of Handling Multiple Exceptions: Java allows handling multiple exceptions
using separate catch blocks for each exception type, or combining them using multi-
catch.
Source code:
Output:

Conclusion:
Learned Exception Handling and Object Oriented Programming Concepts like
encapsulation and inheritance.

You might also like