0% found this document useful (0 votes)
30 views3 pages

Exp 8 J

This document discusses exception handling in Java through try-catch blocks, nested try statements, throw and throws keywords, and custom exceptions. It provides code examples demonstrating each concept and explains how to compile and run Java programs using exception handling.

Uploaded by

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

Exp 8 J

This document discusses exception handling in Java through try-catch blocks, nested try statements, throw and throws keywords, and custom exceptions. It provides code examples demonstrating each concept and explains how to compile and run Java programs using exception handling.

Uploaded by

smdazeem.net2022
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

I'd be glad to help you with the Java Lab Experiment 8 on Exception Handling:

Explanation:

Exception handling is a crucial mechanism in Java that allows you to gracefully manage errors
that might occur during program execution. It prevents unexpected program termination and
enables you to provide meaningful feedback to the user.

There are four key concepts involved:

1. try-catch block:
o The try block encloses the code that might potentially throw an exception.
o The catch block(s) handle specific exceptions thrown within the try block. You can have
multiple catch blocks to handle different exception types.
2. Nested try statements:
o You can have try blocks nested within other try blocks. This allows you to define exception
handling for different parts of your code with finer granularity.
3. throw and throws:
o The throw keyword is used to explicitly throw an exception from within a method.
o The throws keyword is used in a method declaration to indicate the types of exceptions that a
method might throw. This informs the caller that it needs to handle these exceptions
appropriately.
4. Custom exceptions:
o You can create your own exception classes by extending the built-in Exception class. This allows
you to define specialized exceptions that represent specific errors in your application domain.
Code Examples:
(i). Program using try-catch block:
Java
public class TryCatchExample {
public static void main(String[] args) {
int num1 = 10;
int num2 = 0;

try {
int result = num1 / num2; // This line might throw an ArithmeticException
System.out.println("Result: " + result);
} catch (ArithmeticException e) {
System.out.println("Error: Division by zero!");
}

System.out.println("Program continues...");
}
}
(ii). Program with nested try statements:
Java
public class NestedTryExample {
public static void main(String[] args) {
try {
int[] arr = new int[5];
arr[6] = 10; // This line might throw an ArrayIndexOutOfBoundsException

try {
int num = Integer.parseInt("hello"); // This might throw a NumberFormatException
System.out.println("Number: " + num);
} catch (NumberFormatException e) {
System.out.println("Error: Invalid number format!");
}
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Error: Array index out of bounds!");
}

System.out.println("Program continues...");
}
}
(iii). Program using throw and throws:
Java
public class ThrowThrowsExample {
public static int divide(int num1, int num2) throws ArithmeticException {
if (num2 == 0) {
throw new ArithmeticException("Division by zero!"); // Explicitly throwing an exception
}
return num1 / num2;
}

public static void main(String[] args) {


try {
int result = divide(10, 0);
System.out.println("Result: " + result);
} catch (ArithmeticException e) {
System.out.println(e.getMessage()); // Handling the thrown exception
}
}
}
(iv). Program using custom exception:
Java
public class AgeException extends Exception {
public AgeException(String message) {
super(message);
}
}

public class CustomExceptionExample {


public static void validateAge(int age) throws AgeException {
if (age < 0) {
throw new AgeException("Invalid age: Age cannot be negative!");
}
}

public static void main(String[] args) {


try {
validateAge(-5);
} catch (AgeException e) {
System.out.println(e.getMessage());
}
}
}
Remember to compile and run these programs using a Java compiler (e.g., javac) and a Java
runtime environment (JRE).

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