[go: up one dir, main page]

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

Experiment 12

The document outlines a laboratory assignment for Object Oriented Programming using Java, focusing on exception handling. It includes code examples for both built-in exceptions (checked and unchecked) and a user-defined exception class, MarksOutOfBoundsException, to validate input marks. The assignment is part of the academic year 2024-25 for a student named Mohammad Ahmed Shaikh.

Uploaded by

Mohammad Shaikh
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)
3 views5 pages

Experiment 12

The document outlines a laboratory assignment for Object Oriented Programming using Java, focusing on exception handling. It includes code examples for both built-in exceptions (checked and unchecked) and a user-defined exception class, MarksOutOfBoundsException, to validate input marks. The assignment is part of the academic year 2024-25 for a student named Mohammad Ahmed Shaikh.

Uploaded by

Mohammad Shaikh
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
You are on page 1/ 5

Object Oriented Programming using Java Laboratory (DJS23FLES201) Academic Year

2024-25

Name – Mohammad Ahmed Shaikh


Roll no – I169
SAP ID - 60003240302

12. To implement exceptions in Java (read input using DataInputStream/ BufferedReader


classes) (CO1, CO2)

a. WAP to implement inbuild exceptions


i. Checked exceptions (compile time exceptions)
a. ClassNotFoundException
b. IOException ii. Unchecked exceptions (run
time exceptions)
a. NumberFormatException
b. ArithmeticException
c. ArrayIndexOutOfBounds
d. NullPointerException

Code:

import java.io.*;

public class EXP12a { public static void

main(String[] args) {

try {

Class.forName("com.example.NonExistentClass");

} catch (ClassNotFoundException e) {

System.out.println("Checked Exception: " + e);


Object Oriented Programming using Java Laboratory (DJS23FLES201) Academic Year
2024-25

try {

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter something: ");


String input = reader.readLine();
System.out.println("You entered: " + input);

} catch (IOException e) {

System.out.println("Checked Exception: " + e);

try {

String invalidNumber = "abc"; int

num = Integer.parseInt(invalidNumber); }

catch (NumberFormatException e) {

System.out.println("Unchecked Exception: " + e);

} try {

int result = 10 / 0;

} catch (ArithmeticException e) {

System.out.println("Unchecked Exception: " + e);

} try { int[]

arr = new int[2]; int x

= arr[5];
Object Oriented Programming using Java Laboratory (DJS23FLES201) Academic Year
2024-25

} catch (ArrayIndexOutOfBoundsException e) {

System.out.println("Unchecked Exception: " + e); } try {

String str = null; str.length();

} catch (NullPointerException e) {

System.out.println("Unchecked Exception: " + e);

}
}

Output:

b. Write a Java Program to Create a User Defined


Exception class

MarksOutOfBoundsException, If Entered marks of any subject is greater than 100 or less


than 0, and then program should create a user defined Exception of type
MarksOutOfBoundsException and must have a provision to handle it.

Code:
import java.io.*;

class MarksOutOfBoundsException extends Exception {


MarksOutOfBoundsException(String message) { super(message);

}
}
Object Oriented Programming using Java Laboratory (DJS23FLES201) Academic Year
2024-25

public class EXP12b { public static


void main(String[] args) {

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));


try {

System.out.print("Enter marks (0-100): "); int


marks = Integer.parseInt(reader.readLine());

if (marks < 0 || marks > 100) { throw new MarksOutOfBoundsException("Marks


must
be between 0 and 100. You entered: " + marks);
} else {
System.out.println("Valid marks entered: " + marks);
}
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2024-25

} catch (IOException e) {
System.out.println("IO Error: " + e);
} catch (NumberFormatException e) {
System.out.println("Invalid number format.");
} catch (MarksOutOfBoundsException e) {
System.out.println("Custom Exception Caught: " + e.getMessage());
}
}
}

Output:

You might also like