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

Exp 6 Java

The document provides Java programs demonstrating various aspects of exception handling, including basic exception handling with try-catch-finally, multiple catch clauses for different exceptions, built-in exceptions like NullPointerException and NumberFormatException, and a user-defined exception. Each program includes explanations of how exceptions are caught and handled, along with example outputs. The document serves as a comprehensive guide to understanding Java's exception handling mechanism.

Uploaded by

kesavat0001
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)
3 views4 pages

Exp 6 Java

The document provides Java programs demonstrating various aspects of exception handling, including basic exception handling with try-catch-finally, multiple catch clauses for different exceptions, built-in exceptions like NullPointerException and NumberFormatException, and a user-defined exception. Each program includes explanations of how exceptions are caught and handled, along with example outputs. The document serves as a comprehensive guide to understanding Java's exception handling mechanism.

Uploaded by

kesavat0001
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/ 4

A) Write a JAVA program that describes exception handling mechanism

Here’s a simple Java program that demonstrates exception handling using try, catch, and
finally blocks. This program attempts to divide two numbers and catches an
ArithmeticException if division by zero occurs.

Java Exception Handling


public class ExceptionHandlingDemo {
public static void main(String[] args) {
try {
// Attempting to divide by zero
int numerator = 10;
int denominator = 0;
int result = numerator / denominator;
System.out.println("Result: " + result);
} catch (ArithmeticException e) {
// Handling division by zero exception
System.out.println("Exception Caught: Cannot divide by zero.");
} finally {
// Finally block always executes
System.out.println("Execution completed.");
}
}
}

Output:
Exception Caught: Cannot divide by zero.
Execution completed.

Explanation:

 The try block contains code that may throw an exception.


 The catch block catches the ArithmeticException when division by zero is
attempted.
 The finally block executes regardless of whether an exception is thrown or not.

B) Write a JAVA program Illustrating Multiple catch clauses

public class MultipleCatchDemo {


public static void main(String[] args) {
try {
int[] numbers = {10, 20, 30};
int numerator = 10;
int denominator = 0;

// Attempting division by zero


int result = numerator / denominator;
System.out.println("Result: " + result);
// Accessing an invalid index
System.out.println(numbers[5]);
} catch (ArithmeticException e) {
System.out.println("Exception Caught: Cannot divide by zero.");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Exception Caught: Array index is out of bounds.");
} finally {
System.out.println("Execution completed.");
}
}
}

Output:
Exception Caught: Cannot divide by zero.
Execution completed.

OR (if division is fixed and array index issue occurs)

Exception Caught: Array index is out of bounds.


Execution completed.

Explanation:

 The try block contains code that might throw exceptions.


 The first catch block handles ArithmeticException (division by zero).
 The second catch block handles ArrayIndexOutOfBoundsException (invalid array
index access).
 The finally block executes regardless of exceptions, ensuring clean-up or final
statements run.

C) Write a JAVA program for creation of Java Built-in Exceptions

public class BuiltInExceptionDemo {


public static void main(String[] args) {
try {
// NullPointerException Example
String str = null;
System.out.println(str.length());
} catch (NullPointerException e) {
System.out.println("Exception Caught: Null pointer exception.");
}

try {
// NumberFormatException Example
String invalidNumber = "abc";
int num = Integer.parseInt(invalidNumber);
System.out.println(num);
} catch (NumberFormatException e) {
System.out.println("Exception Caught: Number format exception.");
}

try {
// ArrayIndexOutOfBoundsException Example
int[] arr = new int[5];
System.out.println(arr[10]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Exception Caught: Array index out of bounds exception.");
}
}
}

Output:
Exception Caught: Null pointer exception.
Exception Caught: Number format exception.
Exception Caught: Array index out of bounds exception.

Explanation:

 NullPointerException occurs when trying to access a method on a null object.


 NumberFormatException occurs when trying to convert an invalid string into a number.
 ArrayIndexOutOfBoundsException occurs when accessing an invalid array index.

D) Write a JAVA program for creation of User Defined Exception

class CustomException extends Exception {


public CustomException(String message) {
super(message);
}
}

public class UserDefinedExceptionDemo {


public static void validateAge(int age) throws CustomException {
if (age < 18) {
throw new CustomException("Age must be 18 or above.");
} else {
System.out.println("Valid age, access granted.");
}
}

public static void main(String[] args) {


try {
validateAge(16);
} catch (CustomException e) {
System.out.println("Exception Caught: " + e.getMessage());
}
}
}

Output:
Exception Caught: Age must be 18 or above.

Explanation:

 A user-defined exception (CustomException) is created by extending Exception.


 The validateAge() method checks if the age is below 18 and throws the exception if true.
 The try-catch block handles the exception and prints the custom error message.

You might also like