0% found this document useful (0 votes)
2 views2 pages

Exception Handling in Java

Exception handling in Java is a mechanism to manage runtime errors and maintain normal program flow. Common exceptions include ArithmeticException, NullPointerException, NumberFormatException, and StringIndexOutOfBoundsException. Java provides keywords such as try, catch, finally, throw, and throws to handle exceptions effectively.

Uploaded by

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

Exception Handling in Java

Exception handling in Java is a mechanism to manage runtime errors and maintain normal program flow. Common exceptions include ArithmeticException, NullPointerException, NumberFormatException, and StringIndexOutOfBoundsException. Java provides keywords such as try, catch, finally, throw, and throws to handle exceptions effectively.

Uploaded by

rohatgikratagya8
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Exception Handling in Java

Exception: An exception is an unexcepted event that occurs during the execution of a program
and disrupts the normal flow of the program.
Exception handling in Java: is a powerful mechanism to handle runtime errors, ensuring the
normal flow of the application.
Some of the few common exceptions are as follows:

❖ ArithmeticException: occurs when a number (n1) is divided by another number(n2) and


the divisor is zero (n2=0) then Division by zero exception is raised which is one of type
of Arithmetical exception.
❖ NullPointerException: If we have a null value in any variable, performing any operation
on the variable like
String s=null;
[Link]([Link]());
❖ NumberFormatException: occurs If the formatting of any variable or number is
mismatched, it may result into NumberFormatException. Suppose we are expecting a
number from the user and the user inputs a string, will cause NumberFormatException.
ex: int num=[Link](); // user inputs “abc”
❖ StringIndexOfBoundException: occurs when an index on an String object is used
beyond the valid range of indexes (0-length()-1).
ex: String s=”cat”;
[Link]([Link](3);

Java Exception Keywords


Java provides five keywords that are used to handle the exception.
i. try
ii. catch
iii. finally
iv. throw
v. throws
try block: The try block contains code that might generate an exception.
catch block: handles the exception occurred in try block.
they both are always used together i.e try block followed by one or more catch blocks.
Finally Block
The finally block is always executed, regardless of whether an exception is thrown or not. It is
typically used for cleanup code, such as closing files or releasing resources.
The throw keyword is used to explicitly throw an exception.
The throws keyword is used to declare the type of exceptions that might occur within a method.
Example
public class BasicExceptionHandling{
public static void main(String[] args) {
try {
int result = 10 / 0; // This will throw ArithmeticException
} catch (ArithmeticException e) {
[Link]("Caught exception: " + [Link]());
} finally {
[Link]("Finally block executed.");
}
}
}
Difference between throw and throws
throw throws
The throw keyword is followed by an The throws keyword is followed by
instance of Exception to be thrown. class names of Exceptions to be
thrown.
We are allowed to throw only one We can declare multiple exceptions
exception at a time i.e. we cannot using throws keyword that can be
throw multiple exceptions thrown by the method. For example,
main() throws IOException,
SQLException

You might also like