Data Analytics
27K subscribers
1.16K photos
24 videos
26 files
977 links
Dive into the world of Data Analytics – uncover insights, explore trends, and master data-driven decision making.
Download Telegram
Data Analytics
# 📚 Java Programming Language – Part 7/10: Packages & Access Modifiers #Java #Packages #AccessModifiers #Encapsulation Welcome to Part 7 of our Java series! Today we'll explore how to organize code using packages and control visibility with access modifiers.…
# 📚 Java Programming Language – Part 8/10: Exception Handling
#Java #Exceptions #ErrorHandling #Programming

Welcome to Part 8 of our Java series! Today we'll master how to handle errors and exceptional situations in Java programs.

---

## 🔹 What are Exceptions?
Exceptions are events that disrupt normal program flow:
- Checked Exceptions (Compile-time) - Must be handled
- Unchecked Exceptions (Runtime) - Optional handling
- Errors (Serious problems) - Usually unrecoverable

---

## 🔹 Exception Hierarchy
Throwable
├── Error (e.g., OutOfMemoryError)
└── Exception
├── RuntimeException (Unchecked)
│ ├── NullPointerException
│ ├── ArrayIndexOutOfBoundsException
│ └── ArithmeticException
└── Other Exceptions (Checked)
├── IOException
└── SQLException


---

## 🔹 Try-Catch Block
Basic exception handling structure:

try {
// Risky code
int result = 10 / 0;
} catch (ArithmeticException e) {
// Handle specific exception
System.out.println("Cannot divide by zero!");
} catch (Exception e) {
// Generic exception handler
System.out.println("Something went wrong: " + e.getMessage());
} finally {
// Always executes (cleanup code)
System.out.println("Cleanup completed");
}


---

## 🔹 Checked vs Unchecked Exceptions

| Feature | Checked Exceptions | Unchecked Exceptions |
|-----------------|----------------------------|----------------------------|
| Handling | Mandatory (compile error) | Optional |
| Inheritance | Extend Exception | Extend RuntimeException |
| When to Use | Recoverable situations | Programming errors |
| Examples | IOException, SQLException | NullPointerException, ArithmeticException |

---

## 🔹 Throwing Exceptions
### 1. Throw Keyword
public void withdraw(double amount) throws InsufficientFundsException {
if (amount > balance) {
throw new InsufficientFundsException("Not enough balance");
}
balance -= amount;
}


### 2. Throws Clause
public void readFile() throws IOException {
FileReader file = new FileReader("data.txt");
// File operations
}


---

## 🔹 Custom Exceptions
Create your own exception classes:

// Custom checked exception
public class InvalidAgeException extends Exception {
public InvalidAgeException(String message) {
super(message);
}
}

// Custom unchecked exception
public class PaymentFailedException extends RuntimeException {
public PaymentFailedException(String message) {
super(message);
}
}


---

## 🔹 Try-With-Resources
Automatic resource management (Java 7+):

try (FileInputStream fis = new FileInputStream("file.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fis))) {
// Auto-closed after try block
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}


---

## 🔹 Practical Example: Bank Transactions
public class BankAccount {
private double balance;

public void deposit(double amount) throws InvalidAmountException {
if (amount <= 0) {
throw new InvalidAmountException("Deposit amount must be positive");
}
balance += amount;
}

public void withdraw(double amount)
throws InsufficientFundsException, InvalidAmountException {
if (amount <= 0) {
throw new InvalidAmountException("Withdrawal amount must be positive");
}
if (amount > balance) {
throw new InsufficientFundsException("Not enough funds");
}
balance -= amount;
}
}

// Usage:
BankAccount account = new BankAccount();
try {
account.deposit(1000);
account.withdraw(500);
account.withdraw(600); // Throws exception
} catch (InvalidAmountException | InsufficientFundsException e) {
System.err.println("Transaction failed: " + e.getMessage());
}


---
1