SlideShare a Scribd company logo
Unit :- 2
Exception Handling
Object Oriented Programming with Java
An exception is something that is left out or not done on
purpose. An exception to a rule does not follow that rule.
In Java “an event that occurs during the execution of a
program that disrupts the normal flow of instructions” is called an
exception. This is generally an unexpected or unwanted event which
can occur either at compile-time or run-time in application code.
In Short errors and exceptions represent different types of problems that
can occur during program execution. Errors are usually caused by serious
problems that cannot be recovered from, while exceptions are used to
handle recoverable errors within a program.
OOPs with Java_unit2.pdf. sarthak bookkk
Control Flow in Exceptions
The JVM starts the search from the method where the exception
occurred and then goes up the call stack, checking each method in
turn for a catch block that can handle the exception. If a catch block
is found, the JVM transfers control to that block and the exception is
considered to be handled.
JVM Reaction to Exceptions
In Exception handling , we should have
an alternate source through which we
can handle the Exception.
OOPs with Java_unit2.pdf. sarthak bookkk
OOPs with Java_unit2.pdf. sarthak bookkk
OOPs with Java_unit2.pdf. sarthak bookkk
OOPs with Java_unit2.pdf. sarthak bookkk
OOPs with Java_unit2.pdf. sarthak bookkk
OOPs with Java_unit2.pdf. sarthak bookkk
OOPs with Java_unit2.pdf. sarthak bookkk
OOPs with Java_unit2.pdf. sarthak bookkk
OOPs with Java_unit2.pdf. sarthak bookkk
OOPs with Java_unit2.pdf. sarthak bookkk
OOPs with Java_unit2.pdf. sarthak bookkk
Input / Output Basics
OOPs with Java_unit2.pdf. sarthak bookkk
OOPs with Java_unit2.pdf. sarthak bookkk
OOPs with Java_unit2.pdf. sarthak bookkk
Reading and Writing File in Java
Reading from a file:
1. In Java, the FilelnputSream class is used for reading binary data
from a file.
2. It is an input stream that reads bytes from a file in a file system.
3.To read data from a file using FileInputStream, these steps you
need to follow :-
Step 1: Create an instance of the FileInputStream class and pass the path of the file that you
want to read as an argument to its constructor.
Step 2: Create a byte array of a fixed size to read a chunk of data from the file.
Step 3: Use the read() method of the FileInputStream class to read data from the file into the
byte array. This method returns the number of bytes read , or -1 if the end of the file
has been reached.
Step 4: Continue reading data from the file until the read() method returns -1.
Step 5: Close the FileInputStream object using to release the close() method to release any
system resources associated with it.
Following example demonstrates how to use Fileinputstream
to read data from a file :
import java.io.*
public class ReadFileExample {
public static void main(String[] args) {
try {
FileInputStream fileinput = new
FileInputStream ("file.txt");
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = fileInput.read(buffer))!=-1){
System.out.println/new String buffer, 0, bytesread));
}
fileInput.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
In the example above, we create a
FilelnputStream object to read data
from a file called "file.txt".
We then create a byte array of size 1024
to read a chunk of data from the file.
We use the read() method to read data
into the buffer untill the end of the file is
reached, and then we close the
FilelnputStream object.
Finally, we print the data that we read
from the file to the console
Writing in a file: 1. In Java, FileOutputStream class in used for writing binary data to a file.
2. It is an output stream that writes bytes to a file in a file system.
3. To write data to a file using FileOutputStream, you need to follow
these steps:
Step 1: Create an instance of the FileOutputStream class and pass the path of the file that
you want to write as an argument to its constructor. If the file dosen't exist, it will
be created automatically.
Step 2: Create a byte array that contains the date that you want to write to the file.
Step 3: Use the write() method of the FileOutputStream class to write the data to the File
This method writes the entire array to the the file.
Step 4: Close the FileOutputStream object using the close() method to release any system
resources associated with it.
Following example demonstrates how to use FileOutputstream
to write data to a file :
import java.io.*;
public class Write File Example {
public static void main(String[] args) {
try {
FileOutputStream fileOutput = new
FileOutputStream("file.txt");
String data ="This is some data that will be written to a file";
byte[] bytes = data.getBytes();
fileOutput.write(bytes);
fileOutput.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
In the example above, we create a FileOutputStream
object write data to a file called "file.txt".
We then create a string that contains some data that we
wants write to the file.
We convert this string to a byte array using the getbytes()
method and then we use the write() method to write the
data to the file.
Finally, we close the FileOutputStream object.
Multithreading
OOPs with Java_unit2.pdf. sarthak bookkk
OOPs with Java_unit2.pdf. sarthak bookkk
OOPs with Java_unit2.pdf. sarthak bookkk
OOPs with Java_unit2.pdf. sarthak bookkk
OOPs with Java_unit2.pdf. sarthak bookkk
OOPs with Java_unit2.pdf. sarthak bookkk
OOPs with Java_unit2.pdf. sarthak bookkk
OOPs with Java_unit2.pdf. sarthak bookkk
OOPs with Java_unit2.pdf. sarthak bookkk
OOPs with Java_unit2.pdf. sarthak bookkk
OOPs with Java_unit2.pdf. sarthak bookkk
OOPs with Java_unit2.pdf. sarthak bookkk
OOPs with Java_unit2.pdf. sarthak bookkk
OOPs with Java_unit2.pdf. sarthak bookkk
OOPs with Java_unit2.pdf. sarthak bookkk
OOPs with Java_unit2.pdf. sarthak bookkk

More Related Content

PPTX
Input output files in java
PDF
File Handling in Java.pdf
PDF
Basic i/o & file handling in java
PPTX
chapter 2(IO and stream)/chapter 2, IO and stream
PDF
Absolute Java 5th Edition Walter Savitch Solutions Manual
PPTX
Files that are designed to be read by human beings
PDF
JAVA 4.pdfdhfvksfvhsjfbjhdjhbjshjshjvcjdbh
PPTX
File Handling.pptx
Input output files in java
File Handling in Java.pdf
Basic i/o & file handling in java
chapter 2(IO and stream)/chapter 2, IO and stream
Absolute Java 5th Edition Walter Savitch Solutions Manual
Files that are designed to be read by human beings
JAVA 4.pdfdhfvksfvhsjfbjhdjhbjshjshjvcjdbh
File Handling.pptx

Similar to OOPs with Java_unit2.pdf. sarthak bookkk (20)

DOCX
PAGE 1Input output for a file tutorialStreams and File IOI.docx
PPTX
Chapter - 5.pptx
PPT
asdabuydvduyawdyuadauysdasuydyudayudayudaw
PPTX
Java program file I/O
PPTX
IO Programming.pptx all informatiyon ppt
PDF
What is java input and output stream?
PDF
What is java input and output stream?
PDF
Data file handling
PDF
Java I/o streams
PPTX
Java 3 Computer Science.pptx
PPTX
IO and threads Java
PPTX
Basic of Javaio
PPTX
IOStream.pptx
PPTX
C++ - UNIT_-_V.pptx which contains details about File Concepts
DOCX
CS101S. ThompsonUniversity of BridgeportLab 7 Files, File.docx
PPTX
UNIT 5 PY.pptx - FILE HANDLING CONCEPTS
PPT
M251_Meeting 7 (Exception Handling and Text IO).ppt
PDF
Advanced programming ch2
PAGE 1Input output for a file tutorialStreams and File IOI.docx
Chapter - 5.pptx
asdabuydvduyawdyuadauysdasuydyudayudayudaw
Java program file I/O
IO Programming.pptx all informatiyon ppt
What is java input and output stream?
What is java input and output stream?
Data file handling
Java I/o streams
Java 3 Computer Science.pptx
IO and threads Java
Basic of Javaio
IOStream.pptx
C++ - UNIT_-_V.pptx which contains details about File Concepts
CS101S. ThompsonUniversity of BridgeportLab 7 Files, File.docx
UNIT 5 PY.pptx - FILE HANDLING CONCEPTS
M251_Meeting 7 (Exception Handling and Text IO).ppt
Advanced programming ch2
Ad

Recently uploaded (20)

PPTX
AI Strategy room jwfjksfksfjsjsjsjsjfsjfsj
PPTX
Qualitative Qantitative and Mixed Methods.pptx
PPTX
Data_Analytics_and_PowerBI_Presentation.pptx
PPTX
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
PPTX
1_Introduction to advance data techniques.pptx
PDF
“Getting Started with Data Analytics Using R – Concepts, Tools & Case Studies”
PDF
Fluorescence-microscope_Botany_detailed content
PDF
Lecture1 pattern recognition............
PDF
Clinical guidelines as a resource for EBP(1).pdf
PPTX
Computer network topology notes for revision
PDF
Mega Projects Data Mega Projects Data
PPTX
Introduction to machine learning and Linear Models
PDF
Foundation of Data Science unit number two notes
PPTX
Introduction-to-Cloud-ComputingFinal.pptx
PPTX
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx
PPTX
Introduction to Basics of Ethical Hacking and Penetration Testing -Unit No. 1...
PDF
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
PPTX
Supervised vs unsupervised machine learning algorithms
PPTX
Acceptance and paychological effects of mandatory extra coach I classes.pptx
AI Strategy room jwfjksfksfjsjsjsjsjfsjfsj
Qualitative Qantitative and Mixed Methods.pptx
Data_Analytics_and_PowerBI_Presentation.pptx
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
1_Introduction to advance data techniques.pptx
“Getting Started with Data Analytics Using R – Concepts, Tools & Case Studies”
Fluorescence-microscope_Botany_detailed content
Lecture1 pattern recognition............
Clinical guidelines as a resource for EBP(1).pdf
Computer network topology notes for revision
Mega Projects Data Mega Projects Data
Introduction to machine learning and Linear Models
Foundation of Data Science unit number two notes
Introduction-to-Cloud-ComputingFinal.pptx
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx
Introduction to Basics of Ethical Hacking and Penetration Testing -Unit No. 1...
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
Supervised vs unsupervised machine learning algorithms
Acceptance and paychological effects of mandatory extra coach I classes.pptx
Ad

OOPs with Java_unit2.pdf. sarthak bookkk

  • 1. Unit :- 2 Exception Handling Object Oriented Programming with Java An exception is something that is left out or not done on purpose. An exception to a rule does not follow that rule. In Java “an event that occurs during the execution of a program that disrupts the normal flow of instructions” is called an exception. This is generally an unexpected or unwanted event which can occur either at compile-time or run-time in application code.
  • 2. In Short errors and exceptions represent different types of problems that can occur during program execution. Errors are usually caused by serious problems that cannot be recovered from, while exceptions are used to handle recoverable errors within a program.
  • 4. Control Flow in Exceptions
  • 5. The JVM starts the search from the method where the exception occurred and then goes up the call stack, checking each method in turn for a catch block that can handle the exception. If a catch block is found, the JVM transfers control to that block and the exception is considered to be handled. JVM Reaction to Exceptions
  • 6. In Exception handling , we should have an alternate source through which we can handle the Exception.
  • 18. Input / Output Basics
  • 22. Reading and Writing File in Java Reading from a file: 1. In Java, the FilelnputSream class is used for reading binary data from a file. 2. It is an input stream that reads bytes from a file in a file system. 3.To read data from a file using FileInputStream, these steps you need to follow :- Step 1: Create an instance of the FileInputStream class and pass the path of the file that you want to read as an argument to its constructor. Step 2: Create a byte array of a fixed size to read a chunk of data from the file. Step 3: Use the read() method of the FileInputStream class to read data from the file into the byte array. This method returns the number of bytes read , or -1 if the end of the file has been reached. Step 4: Continue reading data from the file until the read() method returns -1. Step 5: Close the FileInputStream object using to release the close() method to release any system resources associated with it.
  • 23. Following example demonstrates how to use Fileinputstream to read data from a file : import java.io.* public class ReadFileExample { public static void main(String[] args) { try { FileInputStream fileinput = new FileInputStream ("file.txt"); byte[] buffer = new byte[1024]; int bytesRead = 0; while ((bytesRead = fileInput.read(buffer))!=-1){ System.out.println/new String buffer, 0, bytesread)); } fileInput.close(); } catch (IOException e) { e.printStackTrace(); } } } In the example above, we create a FilelnputStream object to read data from a file called "file.txt". We then create a byte array of size 1024 to read a chunk of data from the file. We use the read() method to read data into the buffer untill the end of the file is reached, and then we close the FilelnputStream object. Finally, we print the data that we read from the file to the console
  • 24. Writing in a file: 1. In Java, FileOutputStream class in used for writing binary data to a file. 2. It is an output stream that writes bytes to a file in a file system. 3. To write data to a file using FileOutputStream, you need to follow these steps: Step 1: Create an instance of the FileOutputStream class and pass the path of the file that you want to write as an argument to its constructor. If the file dosen't exist, it will be created automatically. Step 2: Create a byte array that contains the date that you want to write to the file. Step 3: Use the write() method of the FileOutputStream class to write the data to the File This method writes the entire array to the the file. Step 4: Close the FileOutputStream object using the close() method to release any system resources associated with it. Following example demonstrates how to use FileOutputstream to write data to a file :
  • 25. import java.io.*; public class Write File Example { public static void main(String[] args) { try { FileOutputStream fileOutput = new FileOutputStream("file.txt"); String data ="This is some data that will be written to a file"; byte[] bytes = data.getBytes(); fileOutput.write(bytes); fileOutput.close(); } catch (IOException e) { e.printStackTrace(); } } } In the example above, we create a FileOutputStream object write data to a file called "file.txt". We then create a string that contains some data that we wants write to the file. We convert this string to a byte array using the getbytes() method and then we use the write() method to write the data to the file. Finally, we close the FileOutputStream object.