Programming in Java

Unit 11: Exceptions, Assertions & Multithreading

From graceful error handling to concurrent execution — master Java's exception hierarchy, assertion mechanisms, and multithreading to build production-grade applications like Zerodha and Paytm.

⏱️ 8 hrs theory + 6 hrs lab  |  💰 ₹15K–₹50K/month  |  📝 30 MCQs (Bloom's Mapped)

💼 Jobs this unlocks: Java Backend Developer (₹6–12 LPA)  |  Multithreaded Systems Engineer (₹8–18 LPA)  |  Fintech Developer (₹10–25 LPA)

Section A

Opening Hook — When 10 Lakh Orders Hit Zerodha at 9:15 AM

🏢 How Zerodha Handles ₹15 Lakh Crore Without Crashing

Every trading day at 9:15 AM, the Indian stock market opens. In that single second, Zerodha processes over 10 lakh concurrent buy/sell orders. Each order is a separate thread. When two traders try to modify the same shared portfolio balance simultaneously, Java's synchronized keyword ensures only one thread touches the balance at a time — preventing ₹15 lakh crore worth of data corruption.

But what happens when a UPI payment to Zerodha fails mid-transaction? A network timeout? An invalid stock symbol? The system doesn't crash — it catches the exception gracefully, logs the error, sends you a friendly "Order failed — please retry" message, and keeps running for the other 9,99,999 orders.

This unit teaches you both pillars: Exceptions (how to handle failures without crashing) and Multithreading (how to run thousands of tasks simultaneously). Together, they make Java the #1 language for fintech, banking, and high-frequency trading systems across India.

🇮🇳 Zerodha🇮🇳 Paytm🇮🇳 PhonePe🇮🇳 Razorpay🇮🇳 HDFC Bank🇮🇳 NSE/BSE
Zerodha's backend is built primarily in Java and Go. Their Kite platform handles 15+ million orders daily with sub-millisecond latency. Every order goes through exception handling (for invalid quantities, insufficient balance, market closed) and multithreading (for concurrent order matching). Nithin Kamath, Zerodha's founder, has stated that robust error handling saved them from ₹100+ crore losses during market volatility events.
Section B

Learning Outcomes — Bloom's Taxonomy Mapped

Bloom's LevelLearning Outcome
🔴 RememberList the exception hierarchy: Throwable → Error/Exception → RuntimeException, and differentiate checked vs unchecked exceptions
🔴 RememberIdentify the five states in the Java thread lifecycle: NEW → RUNNABLE → BLOCKED → WAITING → TERMINATED
🟠 UnderstandExplain the execution flow of try-catch-finally and how try-with-resources auto-closes resources
🟠 UnderstandDescribe how synchronized prevents race conditions in concurrent access to shared data
🟢 ApplyWrite custom checked and unchecked exceptions (InsufficientBalanceException) with exception chaining
🟢 ApplyCreate multithreaded programs using Thread class, Runnable interface, and lambda expressions
🟡 AnalyzeAnalyze deadlock scenarios and determine prevention strategies using lock ordering
🟡 AnalyzeCompare Thread class extension vs Runnable implementation vs lambda-based thread creation
🔵 EvaluateEvaluate when to use ExecutorService thread pools vs manual thread management in production systems
🔵 EvaluateAssess the trade-offs of assert vs exception throwing for input validation in different deployment scenarios
🟣 CreateDesign a producer-consumer system (Zerodha Order Book) using wait/notify with full exception handling
🟣 CreateBuild a multithreaded bank transfer simulation with synchronized blocks and custom exception chains