An EduSpace Notes
UNIT 5: EXCEPTION HANDLING AND MULTITHREADED
PROGRAMMING
5.1 Types of Error
Compile-time Errors:
Mistakes in code syntax (like missing semi-colons, misspelled variable names, or using
keywords incorrectly).
Caught by the compiler before running the program.
Runtime Errors (Exceptions):
Problems during program execution (e.g., dividing by zero, accessing an invalid array
index, or missing files).
Cause exceptions and may crash the program if unhandled.
Logical Errors:
Bugs in the logic or algorithm.
Program runs but produces wrong results; hard to detect.
5.2 Basic Concepts of Exception Handling
Exception: An event that disrupts normal program flow (e.g., an error or unexpected
condition).
Exception Handling: Special coding structure to deal with exceptions and keep
programs running smoothly.
Types of Exceptions:
Built-in: Already available in Java (e.g., ArithmeticException, NullPointerException)
Checked Exception: Checked during compilation (e.g., FileNotFoundException)
Unchecked Exception: Occur during runtime (e.g., division by zero)
User-defined Exception: Custom error type created by extending the Exception class.
Visit” [Link] more Notes of SEM-5
An EduSpace Notes
5.3 Try and Catch Block
try block: Contains code that might throw an exception.
catch block(s): Catches and handles specific exceptions, if they occur.
try {
// risky code
} catch (ExceptionType e) {
// error handling code
Multiple Catch Blocks: Handle di erent types of errors separately.
5.4 Throw and Throws
throw Keyword: Used to create and throw an exception on purpose within your method.
throw new ArithmeticException("Error message");
throws Keyword: Used in the method signature to indicate that the method may throw
an exception, allowing a higher-level part to handle it.
void readFile() throws IOException
5.5 Introduction of Thread
A Thread is a lightweight sub-program capable of running alongside other code.
Lets your application perform multiple tasks at the same time.
Visit” [Link] more Notes of SEM-5
An EduSpace Notes
5.6 Implementation of Thread
Two ways to use threads:
1. Extending the Thread class:
Create a class that extends Thread and override the run() method.
2. Implementing the Runnable interface:
Create a class that implements Runnable and pass it to a Thread object.
5.7 Thread Life Cycle and Methods
A thread can be in several states:
New: Created but not started.
Runnable: Ready to run, but not currently running.
Running: Currently executing.
Blocked/Waiting: Paused, waiting for resources or a signal.
Timed Waiting: Paused for a set time.
Terminated: Finished running.
5.8 Multithreading
Multithreading means running several threads at once, allowing multiple tasks to
happen in parallel.
Improves speed and enables background tasks (e.g., downloading files while using the
app).
Visit” [Link] more Notes of SEM-5