⭐ CHAPTER 4 – Exception & File Handling (
1) Exception & Its Types
An exception is an unexpected event that occurs during the execution of a
program and disrupts its normal flow. Java provides exception handling to
manage such situations and prevent abnormal termination. Exception
handling ensures that errors are handled gracefully, maintaining program
stability and reliability.
Java has two main types of exceptions.
(i) Checked Exceptions: These are checked at compile time. The
programmer must handle them using try-catch or throws. Examples:
IOException, ClassNotFoundException.
(ii) Unchecked Exceptions: These occur at runtime and are subclasses
of RuntimeException. Examples: ArithmeticException,
NullPointerException.
(iii) Errors: Serious problems that cannot be handled by the program, like
OutOfMemoryError.
2) Throws & throw Keyword
The throw keyword is used to manually throw an exception inside the
program. It is commonly used for custom error handling and input validation.
Syntax:
Copy code
Java
Throw new ArithmeticException(“Error message”);
The throws keyword is used in method declaration to indicate that the
method may generate certain exceptions. It shifts the responsibility of
handling exceptions to the caller of the method.
Syntax:
Copy code
Java
Void display() throws IOException { }
3) Create User-Defined Exception
User-defined exceptions allow programmers to define their own exception
classes for specific situations. To create a custom exception, we extend the
Exception class and define a constructor.
Syntax:
Copy code
Java
Class MyException extends Exception {
MyException(String msg){
Super(msg);
These exceptions can be thrown using the throw keyword, allowing
developers to handle application-specific errors more clearly.
4) File Handling – FileInputStream & FileOutputStream
FileInputStream and FileOutputStream are used for reading and writing
binary data.
FileInputStream reads bytes from a file. It is commonly used for reading
images, audio, or binary formats.
FileOutputStream writes bytes into a file and is useful for saving binary data.
Syntax:
Copy code
Java
FileInputStream fin = new FileInputStream(“[Link]”);
FileOutputStream fout = new FileOutputStream(“[Link]”);
These classes work at the byte level and are part of Java’s I/O package.
5) Buffered Stream – Input & Output Stream
Buffered Streams improve the performance of file reading and writing by
using an internal buffer. They reduce the number of disk accesses and
increase program efficiency.
BufferedInputStream provides fast reading of data from files, while
BufferedOutputStream speeds up writing by storing data temporarily before
writing it to the file.
Syntax:
Copy code
Java
BufferedInputStream bin = new BufferedInputStream(new
FileInputStream(“[Link]”));
BufferedOutputStream bout = new BufferedOutputStream(new
FileOutputStream(“[Link]”));
Buffered streams are preferred for large files because they significantly
improve I/O performance.
⭐ CHAPTER 5 – AWT & Swing
1) Difference Between AWT & Swing (Table Format)
AWT
Swing
Uses heavy-weight components (OS-dependent)
Uses lightweight components (OS-independent)
Slower performance
Faster performance
Limited set of UI components
Rich set of advanced components (JButton, JTable, JColorChooser)
Look and feel depends on OS
Look and feel is customizable
No support for pluggable look & feel
Supports multiple look & feel options
2) MVC Architecture in Swing
Swing follows the Model–View–Controller (MVC) architecture to separate
data, UI, and logic.
The Model stores and manages data. It contains the application logic and
state.
The View is responsible for displaying the data visually to the user. In Swing,
components like JLabel, JTable, JButton act as views.
The Controller handles user interaction such as clicking, typing, or selecting.
It interprets user actions, updates the model, and refreshes the view.
This separation makes Swing applications easier to maintain and modify. It
also allows components to behave consistently and support dynamic
updates.
3) Layout Manager / AWT Control – How to Add or Remove a Control
A Layout Manager decides how components are arranged in a container.
Common layout managers are BorderLayout, FlowLayout, and GridLayout.
They automatically position and resize components when the window
changes size.
AWT controls like Button, Label, TextField are added to a container using the
add() method. A control can be removed using the remove() method. Layout
managers simplify positioning by handling the arrangement automatically.
Syntax:
Copy code
Java
Button b = new Button(“OK”);
Frame f = new Frame();
[Link](new FlowLayout());
[Link](b); // add control
[Link](b); // remove control
4) Checkbox, JFileChooser, JColorChooser
A Checkbox allows the user to select multiple options independently. It is part
of AWT and represents a toggle-type control.
Syntax:
Copy code
Java
Checkbox c1 = new Checkbox(“Agree”);
JFileChooser is a Swing component used for choosing files or directories. It
opens a dialog for selecting paths and is widely used in file-handling
applications.
Syntax:
Copy code
Java
JFileChooser fc = new JFileChooser();
[Link](null);
JColorChooser provides a graphical color-selection dialog. It allows users to
pick any color using RGB sliders, palettes, and predefined lists.
Syntax:
Copy code
Java
Color c = [Link](null, “Pick a Color”, [Link]);
4) Event Handling & Listeners and Types
Event handling in Java enables interactive GUI applications. When a user
performs an action such as clicking a button or typing in a text field, an
event Is generated. To handle the event, Java uses listener interfaces. A
listener waits for an event and executes the required code when it occurs.
Common listener types include:
ActionListener – for button clicks
ItemListener – for checkbox/choice selection
MouseListener – for mouse actions
KeyListener – for keyboard input
Syntax:
Copy code
Java
[Link](new ActionListener() {
Public void actionPerformed(ActionEvent e) {
// code here