0% found this document useful (0 votes)
9 views10 pages

OOPS Experiment 5 To 7

The document outlines the implementation of error-handling techniques in Java using exception handling and multithreading. It explains the types of exceptions, keywords used in exception handling, and the advantages of multithreading. Additionally, it provides example programs demonstrating exception handling and multithreading, as well as the use of Java packages and I/O operations.

Uploaded by

ayushvishwkrma1
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)
9 views10 pages

OOPS Experiment 5 To 7

The document outlines the implementation of error-handling techniques in Java using exception handling and multithreading. It explains the types of exceptions, keywords used in exception handling, and the advantages of multithreading. Additionally, it provides example programs demonstrating exception handling and multithreading, as well as the use of Java packages and I/O operations.

Uploaded by

ayushvishwkrma1
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/ 10

Program 5

Objective: Implement error-handling techniques using exception handling and multithreading

Related Theory: exception handling

Exception Handling in Java is one of the effective means to handle runtime errors so that the regular
flow of the application can be preserved. Java Exception Handling is a mechanism to handle runtime
errors such as ClassNotFoundException, IOException, SQLException, RemoteException, etc.

Exception handling in java helps in minimizing exceptions and helps in recovering from exceptions. It
is one of the powerful mechanisms to handle runtime exceptions and makes it bug-free. Exception
handling helps in maintaining the flow of the program. An exception handling is defined as an
abnormal condition that may happen at runtime and disturb the normal flow of the program.

Exception : An expectation is an unexpected event that occurs while executing the program, that
disturbs the normal flow of the code.

Types of exception in Java

• Checked Exceptions

• Those exceptions that are checked at compile-time comprises checked exceptions.

• They are child classes of Exception except for RuntimeException.

• The program will not compile if they are not handled.

• Example: IOException, ClassNotFoundException, etc.

• Unchecked Exceptions

• Those exceptions that are checked at runtime comprises unchecked exceptions.

• They are child classes of RuntimeException.

• They give runtime errors if not handled explicitly.

• Example: ArithmeticException, NullPointerException etc.

• Java Exception Keywords


Exception Handling in java is managed via five keywords: try, catch, throw, throws, and finally.
Here are 5 keywords that are used in handling exceptions in Java

Keyword Description

This keyword is used to specify a block and this block must be followed by either catch
try
or finally. That is, we can’t use try block alone.
This keyword must be preceded by a try block to handle the exception and can be
catch
followed by a final block later.

finally This keyword is used to execute the program, whether an exception is handled or not.

throw This keyword is used to throw an exception.

throws This keyword is used to declare exceptions.

Related Theory: - Multithreading

Multithreading in Java is a process of executing multiple threads simultaneously. A thread is a lightweight


sub-process, the smallest unit of processing. Multiprocessing and multithreading, both are used to achieve
multitasking. However, we use multithreading than multiprocessing because threads use a shared memory
area. They don't allocate separate memory area so saves memory, and context-switching between the
threads takes less time than process. Java Multithreading is mostly used in games, animation, etc.

Advantages of Java Multithreading

1) It doesn't block the user because threads are independent and you can perform multiple
operations at the same time.

2) You can perform many operations together, so it saves time.

3) Threads are independent, so it doesn't affect other threads if an exception occurs in a single
thread.

Thread :A thread is a lightweight subprocess, the smallest unit of processing. It is a separate path of
execution. Threads are independent. If there occurs exception in one thread, it doesn't affect other
threads. It uses a shared memory area.
Program: Exception handling

package exceptionhandlingex;

public class exceptionexample


{
public static void main(String[] args)
{
try {
int x = 10;
int y = 0;
int z = x / y;
}
catch (ArithmeticException e)
{
System.out.println("Arithmetic exception!");
}
System.out.println("Please check the Parameters");
} }
Output:
Program: Multithreading

package multi;

class MultithreadingDemo extends Thread {


public void run()
{
try {

System.out.println(
"Thread " + Thread.currentThread().getId()
+ " is running");
}
catch (Exception e) {
System.out.println("Exception is caught");
}
}
}

public class Multithread {


public static void main(String[] args)
{
int n = 8;
for (int i = 0; i < n; i++) {
MultithreadingDemo object
= new MultithreadingDemo();
object.start();
}
}
}
Output:
Program 6
Objective: Create java program with the use of java packages

Related Theory: Package in Java is a mechanism to encapsulate a group of classes, sub-packages, and
interfaces. All we need to do is put related classes into packages. After that, we can simply write an import
class from existing packages and use it in our program. A package is a container of a group of related
classes where some classes are accessible or exposed and others are kept for internal purposes. We can
reuse existing classes from the packages as many times as we need them in our program. Package names
and directory structure are closely related

Ways: There are two types of packages in java:

1. User-defined Package (Create Your Own Package’s)

2. Built-in packages are packages from the java application programming interface that are the
packages from Java API for example such as swing, util, net, io, AWT, lang, javax, etc.

Program:

package package1;

import java.util.Scanner;

public class packagedemo {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

// Get the user's name


System.out.println("What is your name?");
String name = scanner.nextLine();

// Print a greeting to the user


System.out.println("Hello, " + name + "!");

// Close the scanner


scanner.close();
}
}
Output:
Program 7
Objective: Construct java program using Java I/O package.

Related Theory: Java I/O (Input and Output) is used to process the input and produce the output.

Java uses the concept of a stream to make I/O operation fast. The java.io package contains all the classes
required for input and output operations.

We can perform file handling in Java by Java I/O API.

The below program creates a File object, then creates a FileWriter object to write some data to the file. It
then closes the FileWriter object. Next, it creates a FileReader object to read the data from the file, and
prints it to the console. Finally, it closes the FileReader object.

Program:

import java.io.*;

public class JavaIOExample {

public static void main(String[] args) throws IOException {

// Create a File object


File file = new File("myFile.txt");

// Create a FileWriter object


FileWriter writer = new FileWriter(file);

// Write some data to the file


writer.write("This is some data that I am writing to the file.");

// Close the FileWriter object


writer.close();

// Create a FileReader object


FileReader reader = new FileReader(file);

// Read the data from the file


int i;
while ((i = reader.read()) != -1) {
System.out.print((char) i);
}

// Close the FileReader object


reader.close();
}
}
Output:

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