OOPS Experiment 5 To 7
OOPS Experiment 5 To 7
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.
• Checked Exceptions
• Unchecked Exceptions
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.
1) It doesn't block the user because threads are independent and you can perform multiple
operations at the same 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;
package multi;
System.out.println(
"Thread " + Thread.currentThread().getId()
+ " is running");
}
catch (Exception e) {
System.out.println("Exception is caught");
}
}
}
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
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;
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.
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.*;