0% found this document useful (0 votes)
4 views4 pages

Understanding Exception Handling in Java

Uploaded by

S SMRITI
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)
4 views4 pages

Understanding Exception Handling in Java

Uploaded by

S SMRITI
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:

 Exception = Problem/Error in a program


 Exception Handling = Way to catch and fix that problem
 Helps your program to keep running, even if something goes wrong!

What is Exception Handling?

Exception Handling is like a safety net in a program.


It catches errors when something goes wrong, so your program doesn't crash.

Example:
public class LunchBox {
public static void main(String[] args) {
try {
// Try to open lunch box and eat
String lunch = null;
[Link]([Link]()); // This will cause an error!
} catch (Exception e) {
// If something goes wrong, this block runs
[Link]("Oops! Something went wrong. Let's fix it.");
}
}
}

Logical Example:
import [Link];

public class DivideNumbers {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

[Link]("Enter the first number: ");


int num1 = [Link]();

[Link]("Enter the second number: ");


int num2 = [Link]();

try {
int result = num1 / num2;
[Link]("Result: " + result);
} catch (ArithmeticException e) {
[Link]("You cannot divide by zero! Please try again.");
}

[Link]("Program continues...");
}
}
Try & Catch
Example: Accessing an Array Element
public class ArrayExample {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};

try {
[Link]("Number at position 6 is: " + numbers[5]);
} catch (ArrayIndexOutOfBoundsException e) {
[Link]("Oops! You're trying to access an invalid position in the array.");
}

[Link]("Program still runs after the error!");


}
}
Throw, Throws and finally
Managing excep ons
Throw- Manually creating an exception
public class Exam {
public static void checkCheating(boolean isCheating) {
if (isCheating) {
throw new ArithmeticException("Student is cheating! Raise a complaint.");
} else {
[Link]("Student is honest. Good job!");
}
}

public static void main(String[] args) {


checkCheating(true); // You can try changing to false
}
}
"complaint" is like using throw to raise a specific problem.

Throws – Like a method declaring it might throw an exception


public class BusRide {
public static void startRide() throws InterruptedException {
[Link]("Bus started...");
[Link](2000); // Might throw InterruptedException
[Link]("Bus reached the destination.");
}
public static void main(String[] args) {
try {
startRide();
} catch (InterruptedException e) {
[Link]("Oops! The ride got interrupted.");
}
}
}
Finally => it always runs
public class ExamFinish {
public static void main(String[] args) {
try {
[Link]("Student is writing exam...");
int x = 10 / 0; // Will cause an exception
} catch (Exception e) {
[Link]("Something went wrong during the exam.");
} finally {
[Link]("Collect the answer sheet anyway.");
}
}
}

quick task:

 Write a method that divides two numbers.

 Use throw to raise an error if someone tries to divide by zero.

 Declare the method with throws.

 Use finally to print: 'Thanks for using the calculator!'"

You might also like