0% found this document useful (0 votes)
19 views8 pages

Java Exception Handling Guide

The document provides an overview of exception handling in Java, explaining what exceptions are and how they can be managed to prevent program crashes. It details common exceptions, keywords used in exception handling (try, catch, finally, throw, throws), and includes examples to illustrate the syntax and usage of these keywords. The document emphasizes the importance of writing robust code and handling errors gracefully.

Uploaded by

lilzeeeforreal
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)
19 views8 pages

Java Exception Handling Guide

The document provides an overview of exception handling in Java, explaining what exceptions are and how they can be managed to prevent program crashes. It details common exceptions, keywords used in exception handling (try, catch, finally, throw, throws), and includes examples to illustrate the syntax and usage of these keywords. The document emphasizes the importance of writing robust code and handling errors gracefully.

Uploaded by

lilzeeeforreal
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

[Link] Handling in Java


●​ An exception is an unexpected error that occurs during the execution of a program.​

●​ Exception Handling is a way to detect, handle, and prevent the program from
crashing.​

●​ It ensures the program continues running even when an error occurs.

2. Common Exceptions in Java


Exception Description

ArithmeticException Division by zero

NullPointerException Accessing object with null value

ArrayIndexOutOfBoundsException Accessing invalid array index

NumberFormatException Invalid string to number conversion

ClassNotFoundException Class not found at runtime

3. Use of Exception Handling


●​ To avoid abnormal termination of the program.​

●​ To handle errors gracefully and show user-friendly messages.​

●​ To write robust and safe code.​

4. Exception Handling Keywords


Keyword Purpose

try Code that might cause an exception

catch Code that handles the exception

finally Code that always runs (cleanup)

throw Used to manually throw an exception

throws Declares exceptions that a method might


throw

5. Syntax of Try-Catch Block


try {
// code that may cause an exception
} catch(ExceptionType e) {
// handling code
}

Example 1: Basic Try-Catch


public class Example1 {
public static void main(String[] args) {
try {
int a = 10 / 0;
} catch (ArithmeticException e) {
[Link]("Cannot divide by zero.");
}
}
}

Example 2: Multiple Catch Blocks


public class Example2 {
public static void main(String[] args) {
try {
int[] arr = new int[3];
arr[5] = 100; // error
} catch (ArithmeticException e) {
[Link]("Math error!");
} catch (ArrayIndexOutOfBoundsException e) {
[Link]("Array index out of range.");
}
}
}

Example 3: Finally Block


public class Example3 {
public static void main(String[] args) {
try {
int a = 5 / 0;
} catch (Exception e) {
[Link]("Exception caught.");
} finally {
[Link]("Finally block executed.");
}
}
}

6. Throw and Throws

throw keyword

●​ Used to manually throw an exception.​

throw new ArithmeticException("Manually thrown exception");

throws keyword

●​ Used to declare that a method might throw an exception.​

public void checkAge(int age) throws ArithmeticException {


if (age < 18) {
throw new ArithmeticException("Underage");
}
}

Difference between throw and throws

1. Example of throw

throw is used inside a method to actually throw an exception


object.

class ThrowExample {
public static void main(String[] args) {

int age = 15;

if(age < 18) {

// using throw to create and throw an exception

throw new ArithmeticException("Not eligible to vote!");

} else {

[Link]("Eligible to vote!");

Explanation:

●​ throw keyword is followed by an exception object.​

●​ Here, if age is less than 18, we throw an


ArithmeticException.​
●​ This is the actual act of throwing an exception.​

2. Example of throws

throws is used in the method declaration to tell that this


method might throw an exception.​
Caller method should handle it.

class ThrowsExample {

// method declares that it may throw an exception

static void checkFile() throws [Link] {

throw new [Link]("File not found!");

public static void main(String[] args) {

try {

checkFile(); // calling method that declares throws

} catch ([Link] e) {
[Link]("Exception handled: " +
[Link]());

Explanation:

●​ throws keyword is used in method declaration.​

●​ It does not throw the exception itself; it just declares that


this method might throw.​

●​ Actual throwing happens inside the method using throw.​

●​ Caller (main method here) must handle the exception


using try-catch.​

●​ throw → used inside a method to actually throw an


exception object.​
●​ throws → used in the method signature to declare the
possibility of an exception.​

Concept Description

try Place code that might cause an exception

catch Catches and handles specific exception

finally Always runs (used for closing resources)

throw Manually throw an exception

throws Declares exceptions from a method

You might also like