[go: up one dir, main page]

0% found this document useful (0 votes)
2 views7 pages

OOPJ lab programs

The document provides examples of error handling in Java using exception handling and multithreading techniques. It includes code snippets for handling array index exceptions and runtime exceptions in threads, as well as creating and using Java packages for student information. Additionally, it demonstrates Java I/O operations for writing to and reading from a file.

Uploaded by

rabbitgupta0606
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)
2 views7 pages

OOPJ lab programs

The document provides examples of error handling in Java using exception handling and multithreading techniques. It includes code snippets for handling array index exceptions and runtime exceptions in threads, as well as creating and using Java packages for student information. Additionally, it demonstrates Java I/O operations for writing to and reading from a file.

Uploaded by

rabbitgupta0606
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/ 7

5.

Implement error-handling techniques using exception handling and multithreading

Error Handling Using Exception Handling

Input:

public class Excep {

public static void main(String[] args) {

try {

int[] arr = {1, 2, 3};

System.out.println(arr[5]); // This will throw ArrayIndexOutOfBoundsException

} catch (ArrayIndexOutOfBoundsException e) {

System.out.println("Error: Array index is out of bounds!");

} catch (Exception e) {

System.out.println("Generic Error: " + e.getMessage());

} finally {

System.out.println("This will always execute.");

Output:

Error: Array index is out of bounds!

This will always execute.


Error Handling Using Mutlithreading

Input:

class MyTask extends Thread {

private int number;

public MyTask(int number) {

this.number = number;

public void run() {

try {

System.out.println("Thread " + number + " started.");

if (number == 3) {

throw new RuntimeException("Intentional error in Thread 3");

Thread.sleep(1000);

System.out.println("Thread " + number + " finished.");

} catch (InterruptedException e) {

System.out.println("Thread " + number + " was interrupted.");

} catch (RuntimeException e) {

System.out.println("Caught exception in Thread " + number + ": " + e.getMessage());

}
public class MultiThreadExceptionHandling {

public static void main(String[] args) {

for (int i = 1; i <= 5; i++) {

Thread t = new MyTask(i);

t.start();

Output:

Thread 4 started.

Thread 2 started.

Thread 1 started.

Thread 5 started.

Thread 3 started.

Caught exception in Thread 3: Intentional error in Thread 3

Thread 2 finished.

Thread 4 finished.

Thread 5 finished.

Thread 1 finished.
6. Create java program with the use of java packages.

Input:

StudInfo.java

package StudentInfo;

public class StudInfo {

private final String name;

private final int age;

private final String email;

public StudInfo(String name, int age, String email) {

this.name=name;

this.age=age;

this.email=email;

public String getName() {

return name;

public int getAge() {

return age;

public String getEmail() {


return email;

public void displayInfo() {

System.out.println("Student Name: "+name);

System.out.println("Student Age: "+age);

System.out.println("Student Email: "+email);

MainApp.java

import StudentInfo.StudInfo;

public class MainApp {

public static void main(String[] args) {

StudInfo s = new StudInfo("Alicia",23,"aliciaberenson@xyz.com");

s.displayInfo();

Output:

PS C:\Users\Student\Desktop\oopj> javac StudentInfo/StudInfo.java MainApp.java

PS C:\Users\Student\Desktop\oopj> java MainApp

Student Name: Alicia

Student Age: 23

Student Email: aliciaberenson@xyz.com


7. Construct java program using Java I/O package

Input:

import java.io.*;

public class IO {

public static void main(String[] args) {

String fileName = "ex.txt";

String content = "Hello, this is an example of Java I/O operations.";

try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))) {

writer.write(content);

System.out.println("FIle Written Successfully.");

} catch (IOException e) {

System.err.println("Error writing to file: "+e.getMessage());

try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {

String line;

System.out.println("File content: ");

while((line=reader.readLine())!=null) {

System.out.println(line);

} catch(IOException e) {

System.err.println("Error reading from file: "+e.getMessage());

}
Output:

FIle Written Successfully.

File content:

Hello, this is an example of Java I/O operations.

You might also like