0% found this document useful (0 votes)
3 views64 pages

Module 3 Part 2

Module 3 covers Java packages, exception handling, and multithreading. It explains the concept of sub-packages, their independence, and the importance of import statements while addressing common misconceptions about package access. Additionally, it discusses the necessity of exception handling to manage runtime errors and improve program reliability.
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)
3 views64 pages

Module 3 Part 2

Module 3 covers Java packages, exception handling, and multithreading. It explains the concept of sub-packages, their independence, and the importance of import statements while addressing common misconceptions about package access. Additionally, it discusses the necessity of exception handling to manage runtime errors and improve program reliability.
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

Module 3: Packages, Exception Handling and

Multithreading

Premanand S

Assistant Professor
School of Electronics Engineering
Vellore Institute of Technology
Chennai Campus
premanand.s@[Link]

March 9, 2026

Premanand S (VIT-CC) Module 3 March 9, 2026 1 / 36


What is a Sub-Package?

A sub-package is a package defined inside another package using dot


notation.
Example:

Example
package [Link];

Java allows hierarchical naming using dots (.)


Each level represents a folder in directory structure
Important: Sub-packages are NOT automatically related in terms of
access control.

Premanand S (VIT-CC) Module 3 March 9, 2026 2 / 36


Folder Structure Representation

Package Declaration
package [Link];

Corresponding Directory Structure:

com/
|
+-- company/
|
+-- project/

JVM class loader searches using this folder hierarchy


Package name must match directory structure exactly

Premanand S (VIT-CC) Module 3 March 9, 2026 3 / 36


Example: Creating a Sub-Package

File: com/company/project/[Link]

package [Link];

public class Employee {


public void display() {
[Link]("Employee Class");
}
}

Premanand S (VIT-CC) Module 3 March 9, 2026 4 / 36


Accessing a Sub-Package Class

File: [Link]

import [Link];

public class MainApp {


public static void main(String[] args) {
Employee e = new Employee();
[Link]();
}
}

Must import using full package path


Folder hierarchy must exist before compilation

Premanand S (VIT-CC) Module 3 March 9, 2026 5 / 36


Very Important Concept (Common Misconception)

Question:
If we have:
package [Link];
package [Link];
Are they considered the same package?

Premanand S (VIT-CC) Module 3 March 9, 2026 6 / 36


Very Important Concept (Common Misconception)

Question:
If we have:
package [Link];
package [Link];
Are they considered the same package?
Answer: No.

Premanand S (VIT-CC) Module 3 March 9, 2026 6 / 36


Very Important Concept (Common Misconception)

Question:
If we have:
package [Link];
package [Link];
Are they considered the same package?
Answer: No.
Sub-package is a completely separate package
Default access does NOT work between them

Premanand S (VIT-CC) Module 3 March 9, 2026 6 / 36


Misconception Demo Code
File: com/company/[Link]
package [Link];
public class A {
int x = 100; // defaultaccess }

File: com/company/project/[Link]
package [Link];
import [Link].A;
public class B {
public static void main(String[] args) {
A obj = new A();
[Link](obj.x); // Will this work?
} }

Premanand S (VIT-CC) Module 3 March 9, 2026 7 / 36


Misconception Demo Code
File: com/company/[Link]
package [Link];
public class A {
int x = 100; // defaultaccess }

File: com/company/project/[Link]
package [Link];
import [Link].A;
public class B {
public static void main(String[] args) {
A obj = new A();
[Link](obj.x); // Will this work?
} }

Answer: Error (Different package)


Premanand S (VIT-CC) Module 3 March 9, 2026 7 / 36
Why Java Uses Hierarchical Packages

Avoid global naming conflicts


Support large-scale enterprise applications
Enable modular project organization
Align with company domain names

Reverse Domain Naming Convention:


[Link]
[Link]
[Link]

Premanand S (VIT-CC) Module 3 March 9, 2026 8 / 36


Compilation Using Command Prompt

javac -d . com/company/project/[Link]
javac [Link]
java MainApp

-d . creates proper directory structure


Always compile from project root folder

Premanand S (VIT-CC) Module 3 March 9, 2026 9 / 36


Why Do We Need Import Statements?

Java programs often use classes from other packages


Import statements allow us to use those classes easily
Without import, we must use the fully qualified class name

Example
[Link]

Instead of writing the full name every time, we use import

Premanand S (VIT-CC) Module 3 March 9, 2026 10 / 36


Importing a Specific Class

Syntax
import package_name.ClassName;

Example

import [Link];

public class Test {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
}
}

Only the specified class is available

Premanand S (VIT-CC) Module 3 March 9, 2026 11 / 36


Importing All Classes in a Package

Syntax
import package_name.*;

Example

import [Link].*;

public class Example {


public static void main(String[] args) {
ArrayList list = new ArrayList();
Scanner sc = new Scanner([Link]);
}
}

Allows access to all public classes in that package


Premanand S (VIT-CC) Module 3 March 9, 2026 12 / 36
Important Clarification

Common Misconception

Premanand S (VIT-CC) Module 3 March 9, 2026 13 / 36


Important Clarification

Common Misconception
import [Link].*; does NOT load all classes into memory

Premanand S (VIT-CC) Module 3 March 9, 2026 13 / 36


Important Clarification

Common Misconception
import [Link].*; does NOT load all classes into memory
The compiler only loads classes when they are actually used

Premanand S (VIT-CC) Module 3 March 9, 2026 13 / 36


Important Clarification

Common Misconception
import [Link].*; does NOT load all classes into memory
The compiler only loads classes when they are actually used
It simply tells the compiler where to search for class definitions

Premanand S (VIT-CC) Module 3 March 9, 2026 13 / 36


Using Fully Qualified Class Name

Without Import Statement

public class Demo {


public static void main(String[] args) {
[Link] sc =
new [Link]([Link]);
}
}

Full package path must be specified


Import statements make code shorter and readable

Premanand S (VIT-CC) Module 3 March 9, 2026 14 / 36


Name Conflict Problem

Two packages may contain classes with the same name.

import [Link];
import [Link];

Premanand S (VIT-CC) Module 3 March 9, 2026 15 / 36


Name Conflict Problem

Two packages may contain classes with the same name.

import [Link];
import [Link];

Question: Which Date class should Java use?

Premanand S (VIT-CC) Module 3 March 9, 2026 15 / 36


Name Conflict Problem

Two packages may contain classes with the same name.

import [Link];
import [Link];

Question: Which Date class should Java use?


Answer: Compiler error due to ambiguity.

Premanand S (VIT-CC) Module 3 March 9, 2026 15 / 36


Resolving Name Conflicts
Use the fully qualified class name.

import [Link];

public class Test {


public static void main(String[] args) {

[Link] d1 =
new [Link]();

[Link] d2 =
new [Link](0);
}
}

Fully qualified names remove ambiguity


Premanand S (VIT-CC) Module 3 March 9, 2026 16 / 36
Import Variations Summary

Import Type Example


Specific class import import [Link];
All classes in package import [Link].*;
Fully qualified name [Link]

Premanand S (VIT-CC) Module 3 March 9, 2026 17 / 36


Key Takeaways

Sub-packages are independent packages in Java


Folder structure must match the package declaration
Default access works only within the same package
Protected allows subclass access across packages
Hierarchical naming supports scalable software design

Memory Rule:

Package hierarchy ̸= Access hierarchy

Premanand S (VIT-CC) Module 3 March 9, 2026 18 / 36


From Packages to Exception Handling
What We Learned So Far
Organizing classes using packages
Creating and accessing packages
Sub-packages and hierarchical structure
Access modifiers across packages
Import statements and name conflict resolution

Premanand S (VIT-CC) Module 3 March 9, 2026 19 / 36


From Packages to Exception Handling
What We Learned So Far
Organizing classes using packages
Creating and accessing packages
Sub-packages and hierarchical structure
Access modifiers across packages
Import statements and name conflict resolution

But a question remains...

Premanand S (VIT-CC) Module 3 March 9, 2026 19 / 36


From Packages to Exception Handling
What We Learned So Far
Organizing classes using packages
Creating and accessing packages
Sub-packages and hierarchical structure
Access modifiers across packages
Import statements and name conflict resolution

But a question remains...


What happens when something goes wrong during program
execution?
File not found
Invalid user input
Division by zero
Network failure

Premanand S (VIT-CC) Module 3 March 9, 2026 19 / 36


From Packages to Exception Handling
What We Learned So Far
Organizing classes using packages
Creating and accessing packages
Sub-packages and hierarchical structure
Access modifiers across packages
Import statements and name conflict resolution

But a question remains...


What happens when something goes wrong during program
execution?
File not found
Invalid user input
Division by zero
Network failure
Java provides a mechanism to handle such situations:
Premanand S (VIT-CC) Module 3 March 9, 2026 19 / 36
A Simple Situation

Consider the following program

int a = 10;
int b = 0;
int result = a / b;
[Link](result);

Premanand S (VIT-CC) Module 3 March 9, 2026 20 / 36


A Simple Situation

Consider the following program

int a = 10;
int b = 0;
int result = a / b;
[Link](result);

What will happen when this program runs?

Premanand S (VIT-CC) Module 3 March 9, 2026 20 / 36


A Simple Situation

Consider the following program

int a = 10;
int b = 0;
int result = a / b;
[Link](result);

What will happen when this program runs?


The program crashes
JVM stops execution
Remaining code does not execute

Premanand S (VIT-CC) Module 3 March 9, 2026 20 / 36


A Simple Situation

Consider the following program

int a = 10;
int b = 0;
int result = a / b;
[Link](result);

What will happen when this program runs?


The program crashes
JVM stops execution
Remaining code does not execute

Problem
Programs may fail during execution due to unexpected situations.

Premanand S (VIT-CC) Module 3 March 9, 2026 20 / 36


What is an Exception?

An Exception is an abnormal condition that occurs during program


execution.

Premanand S (VIT-CC) Module 3 March 9, 2026 21 / 36


What is an Exception?

An Exception is an abnormal condition that occurs during program


execution.
It interrupts the normal flow of the program.

Premanand S (VIT-CC) Module 3 March 9, 2026 21 / 36


What is an Exception?

An Exception is an abnormal condition that occurs during program


execution.
It interrupts the normal flow of the program.
Exceptions occur at runtime.

Examples
Division by zero
File not found
Invalid array index
Invalid user input

Premanand S (VIT-CC) Module 3 March 9, 2026 21 / 36


Why Do We Need Exception Handling?

Without exception handling:


Program terminates abruptly
Remaining code does not execute
Poor user experience
Difficult debugging

Premanand S (VIT-CC) Module 3 March 9, 2026 22 / 36


Why Do We Need Exception Handling?

Without exception handling:


Program terminates abruptly
Remaining code does not execute
Poor user experience
Difficult debugging
With exception handling:
Program handles errors gracefully
Execution continues
Error messages can be displayed
Improves reliability

Premanand S (VIT-CC) Module 3 March 9, 2026 22 / 36


Example Without Exception Handling
public class Test {
public static void main(String[] args) {

int a = 10;
int b = 0;

int result = a / b;

[Link](result);
[Link]("Program Finished");
}
}

Premanand S (VIT-CC) Module 3 March 9, 2026 23 / 36


Example Without Exception Handling
public class Test {
public static void main(String[] args) {

int a = 10;
int b = 0;

int result = a / b;

[Link](result);
[Link]("Program Finished");
}
}

Output
Exception in thread "main"
[Link]
Premanand S (VIT-CC) Module 3 March 9, 2026 23 / 36
Handling the Exception
public class Test {
public static void main(String[] args) {

int a = 10;
int b = 0;

try {
int result = a / b;
[Link](result);
}

catch (ArithmeticException e) {
[Link]("Cannot divide by zero");
}
[Link]("Program continues...");
} }
Premanand S (VIT-CC) Module 3 March 9, 2026 24 / 36
Exception Handling Flow

1 Program starts execution

Premanand S (VIT-CC) Module 3 March 9, 2026 25 / 36


Exception Handling Flow

1 Program starts execution


2 Error occurs inside try block

Premanand S (VIT-CC) Module 3 March 9, 2026 25 / 36


Exception Handling Flow

1 Program starts execution


2 Error occurs inside try block
3 JVM creates an exception object

Premanand S (VIT-CC) Module 3 March 9, 2026 25 / 36


Exception Handling Flow

1 Program starts execution


2 Error occurs inside try block
3 JVM creates an exception object
4 Control moves to catch block

Premanand S (VIT-CC) Module 3 March 9, 2026 25 / 36


Exception Handling Flow

1 Program starts execution


2 Error occurs inside try block
3 JVM creates an exception object
4 Control moves to catch block
5 Error is handled

Premanand S (VIT-CC) Module 3 March 9, 2026 25 / 36


Exception Handling Flow

1 Program starts execution


2 Error occurs inside try block
3 JVM creates an exception object
4 Control moves to catch block
5 Error is handled
6 Program continues execution

Premanand S (VIT-CC) Module 3 March 9, 2026 25 / 36


Structure of Exception Handling
try {

// risky code

catch(ExceptionType e) {

// handling code

finally {

// always executes
}
Premanand S (VIT-CC) Module 3 March 9, 2026 26 / 36
Finally Block

Purpose
Code that must always execute

Premanand S (VIT-CC) Module 3 March 9, 2026 27 / 36


Finally Block

Purpose
Code that must always execute
Used for resource cleanup

Premanand S (VIT-CC) Module 3 March 9, 2026 27 / 36


Finally Block

Purpose
Code that must always execute
Used for resource cleanup
Examples:
Closing files
Closing database connections

Premanand S (VIT-CC) Module 3 March 9, 2026 27 / 36


User Defined Exception
class InvalidAgeException extends Exception {

InvalidAgeException(String message) {
super(message);
}
}

public class Test {


static void checkAge(int age)
throws InvalidAgeException {

if(age < 18)


throw new InvalidAgeException(
"Not eligible");
}
}
Premanand S (VIT-CC) Module 3 March 9, 2026 28 / 36
Using throw
Purpose:
Used to explicitly throw an exception
Used inside a method or block
Example
public class Test {
public static void main(String[] args) {
int age = 15;
if(age < 18){
throw new ArithmeticException(
"Not eligible to vote");
}
[Link]("Eligible to vote");
} }

Premanand S (VIT-CC) Module 3 March 9, 2026 29 / 36


Using throw
Purpose:
Used to explicitly throw an exception
Used inside a method or block
Example
public class Test {
public static void main(String[] args) {
int age = 15;
if(age < 18){
throw new ArithmeticException(
"Not eligible to vote");
}
[Link]("Eligible to vote");
} }

If age is less than 18, program throws an exception


JVM stops normal execution
Premanand S (VIT-CC) Module 3 March 9, 2026 29 / 36
Using throws
Purpose:
Used to declare exceptions
Written in method signature
Example
import [Link].*;
public class Test {
static void readFile() throws IOException {
FileReader file =
new FileReader("[Link]");
}
public static void main(String[] args) {
try{
readFile();
}
catch(IOException e){
[Link]("File not found");
} } }
Premanand S (VIT-CC) Module 3 March 9, 2026 30 / 36
throw vs throws

throw throws
Used to throw exception Used to declare exception
Used inside method Used in method signature
Throws one exception Can declare multiple exceptions

Premanand S (VIT-CC) Module 3 March 9, 2026 31 / 36


User Defined Exceptions

Java allows programmers to create their own exceptions.

Premanand S (VIT-CC) Module 3 March 9, 2026 32 / 36


User Defined Exceptions

Java allows programmers to create their own exceptions.


These are called User Defined Exceptions or Custom Exceptions.

Premanand S (VIT-CC) Module 3 March 9, 2026 32 / 36


User Defined Exceptions

Java allows programmers to create their own exceptions.


These are called User Defined Exceptions or Custom Exceptions.
They are created by extending the Exception class.

Premanand S (VIT-CC) Module 3 March 9, 2026 32 / 36


Syntax of User Defined Exception

class MyException extends Exception {

MyException(String message) {
super(message);
}

Custom exception class extends Exception


Constructor passes message to parent class

Premanand S (VIT-CC) Module 3 March 9, 2026 33 / 36


Example: Invalid Age Exception
class InvalidAgeException extends Exception {
InvalidAgeException(String msg){
super(msg);
} }
public class Test {
static void checkAge(int age)
throws InvalidAgeException {
if(age < 18)
throw new InvalidAgeException("Not eligible to vote");
[Link]("Eligible to vote"); }
public static void main(String[] args) {
try{
checkAge(16); }
catch(InvalidAgeException e){
[Link]([Link]());
} } }
Premanand S (VIT-CC) Module 3 March 9, 2026 34 / 36
Handling Custom Exception

public class Main {

public static void main(String[] args) {

try{
[Link](16);
}

catch(InvalidAgeException e){
[Link]([Link]());
}

}
}

Premanand S (VIT-CC) Module 3 March 9, 2026 35 / 36


Thank You!
Stay Connected

Premanand S
Email: premanand.s@[Link]
Phone: +91-7358679961

LinkedIn: [Link]/in/premsanand
Instagram: [Link]/premsanand
WhatsApp Channel: anandsDataX

Google Scholar: Google Scholar Profile


GitHub: [Link]/anandprems

Premanand S (VIT-CC) Module 3 March 9, 2026 36 / 36

You might also like