Exception Handling & Debugging

💻 Exception Handling & Debugging – Mini Study Guide


🔎 PART 1: Debugging Concepts and Techniques

🧠 What is Debugging?

Debugging = the process of finding and fixing errors (bugs) in a program that causes unexpected behavior.

This part is important kasi ito ‘yung foundation ng topic. If you can’t debug, you can’t survive programming 😭.

According to the handout

  • It identifies errors

  • It fixes unexpected behavior

  • It’s one of the most time-consuming parts of coding

Think of debugging like being a detective. 🕵‍♂️
Something’s wrong → you investigate → you find the culprit → you fix it.


🔁 The Typical Debugging Process

1⃣ Examine the Error Symptoms
  • Look at crashes

  • Read error messages

  • Observe weird behavior

Example:
If your calculator app suddenly closes when dividing by 0, that’s a symptom.


2⃣ Identify the Cause
  • Trace the code execution

  • Find the exact line causing the issue

Example:
You realize the denominator was 0 → boom 💥 division by zero.


3⃣ Fix the Error
  • Modify the code

  • Test again

  • Make sure it works properly

In short:

See the problem → Find the cause → Fix it


🛠 Debugging Techniques (Super Important 🔥)

1⃣ Understanding the Problem

Before changing code, understand it muna.

If you don’t understand the problem:

  • You waste time

  • You create new bugs

💡 Reproduce the issue first.


2⃣ Backtracking (Backward Debugging)

Start from where the error happened → trace backward.

Good for:

  • Complex systems

  • Hard-to-trace bugs

Parang reverse engineering.


3⃣ Debugging Tools

Tools help you:

  • Inspect variables

  • Check memory usage

  • Monitor execution

Examples from the handout:

  • Chrome DevTools

  • Testsigma

  • dbForger

4⃣ Breakpoints & Stepping

Breakpoint

Stops program execution temporarily.

Why?
To inspect:

  • Variable values

  • Program state

Stepping

Move through code line by line manually.

This is like slow-mo mode for your program 🐢.


5⃣ Binary Search Debugging

Divide code into halves.
Test each half.
Narrow down the issue.

Best for:

  • Large codebases

  • Hard-to-find bugs


6⃣ Rubber Ducking 🦆

Explain your code out loud.

Yes, literally.

When you explain your logic clearly, you often realize:
“Omg, mali pala dito.”


7⃣ Log Analysis

Insert log statements (e.g., print statements).

Check:

  • Variable values

  • Execution flow


8⃣ Clustering Bugs

Group similar bugs together.

Fixing one might fix others.

Efficiency level = 📈


9⃣ Take Breaks

Your brain isn’t a robot.

Sometimes stepping away = clarity.


🔟 Take Notes

Document:

  • What failed

  • What worked

  • What you changed

Future you will thank you.


🔥 Debugging Summary Mnemonic:

U B D B B R L C B N

Understanding
Backtracking
Debug tools
Breakpoints
Binary search
Rubber duck
Log analysis
Clustering
Break
Notes


PART 2: Basic Exception Handling

🤔 What is an Exception?

An exception is an unexpected event during program execution.

Examples:

  • File doesn’t exist

  • Disk full

  • Invalid user input

  • Divide by zero

  • Invalid array index

Important:
Not all exceptions are “bad” — they’re just unusual conditions.


🕒 Runtime Error vs Syntax Error

Type

When Detected

Example

Syntax Error

During compilation

Missing semicolon

Runtime Error

During execution

Divide by zero


🚨 Error Class vs Exception Class (Java)

Java has 2 main categories:

Error

Exception

Serious problem

Less serious

Usually unrecoverable

Can recover

Example: OutOfMemoryError

Example: ArithmeticException

Think of it like:

  • Error = System level problem 💀

  • Exception = Program-level issue you can handle 💪


🌳 Exception Hierarchy (Important for Exams)

From page 3:

Object
→ Throwable
 → Exception
 → Error

So:

All exceptions and errors come from Throwable.


Common Exceptions You MUST Know

🔢 ArithmeticException

Division by zero.

📏 IndexOutOfBoundsException

Invalid index in array/list/string.

📥 InputMismatchException

Wrong data type input.

Example:
User enters "three" instead of 3.

💾 IOException

File handling issues.


💻 Division Program Example (From Handout)

Original code divides numerator by denominator.

Problem scenarios:

Execution

Input

Result

#1

12 / 4

Works

#2

12 / 0

ArithmeticException

#3

12 / "three"

InputMismatchException

When denominator = 0 → crash 💥
When input is not integer → crash 💥

That crash message list is called:
👉 Stack Trace

🧩 try, catch, finally Blocks

Now this is the heart of exception handling.


🔹 try Block

Used when:
You know something risky might happen.

Structure:

try {
   risky code
}

🔹 catch Block

Handles the exception.

Structure:

catch(ExceptionType e) {
   handle error
}

Important:

  • It’s not a method.

  • It catches thrown exceptions.


🔹 finally Block

This runs:

  • Whether error happens or not

  • Always executes

Used for:

  • Closing files

  • Releasing resources

  • Cleanup

Example from handout:

Even if file reading fails, the file still closes in finally.

🔁 try-catch Flow (Simple Explanation)

  1. Try runs

  2. If no error → skip catch

  3. If error → jump to catch

  4. Finally always runs


🧠 Visual Logic Flow

try {
   risky code
}
catch(Exception e) {
   handle error
}
finally {
   cleanup
}

Think of it like:

Try = attempt

Catch = damage control

Finally = clean your mess


🎯 Quick Comparison: Without vs With try-catch

Without try-catch

With try-catch

Program crashes

Program continues

Stack trace only

Custom error message

Bad user experience

Controlled behavior


🧠 FINAL RECAP (Memory Tricks)

🔥 Debugging = 3 Steps

SEE – FIND – FIX


🔥 Exceptions

Unexpected events during execution.


🔥 Java Error Types

Error = unrecoverable

Exception = recoverable

Mnemonic:

E = End (serious)

Ex = Explainable (manageable)


🔥 try-catch-finally

Try = Risk

Catch = Handle

Finally = Cleanup

Mnemonic:

TCF = Try Catch Finish


🔥 Most Common Exceptions to Remember

AIIIO

Arithmetic

IndexOutOfBounds

InputMismatch

IOException

OutOfMemoryError