DEV Community

Hari R
Hari R

Posted on

Understanding Try-Catch: Throw vs. No Throw

What is try–catch?

Imagine you're writing code that might crash — for example, dividing by zero or opening a file that doesn’t exist.

A try block says:
🗣️ “Hey, computer, try running this code. But if something goes wrong… don’t panic. Jump to the catch block and handle it.”

Real-life analogy:
You're trying to open a door with a key. If the key breaks (try fails), you immediately call the locksmith (catch block).

🧪 Basic Code Example:

try {
    int result = 10 / 0;  // This will crash: divide by zero
}
catch (DivideByZeroException ex) {
    Console.WriteLine("Oops! You tried to divide by zero.");
}
Enter fullscreen mode Exit fullscreen mode

No crash. Just a friendly message.
The program survives and keeps running.

🚨 2. What is throw?

Now imagine you want to signal that something has gone wrong — maybe someone passed bad data to your method.

A throw is like raising your hand and saying:
🗣️ “This is not okay. I’m throwing this problem up to someone else to handle!”

💣 Example:

if (number < 0) {
    throw new ArgumentException("Number must be non-negative");
}
Enter fullscreen mode Exit fullscreen mode

⚠️ Why would you want to throw?
Because:
You spotted a bad situation
You don’t know how to handle it right here
You want someone else (maybe catch) to handle it

When NOT to throw

Let’s say you catch an error — but instead of escalating it, you just log it and move on. You don’t throw again.

That’s OK when:
The error isn’t critical
You can recover from it
Or... you're logging the error and don’t want to crash the app

Example:

try {
    var result = GetDataFromFile("missing.txt");
}
catch (FileNotFoundException ex) {
    Console.WriteLine("File not found. Skipping this part.");
    // No throw here — we're handling it gracefully
}
Enter fullscreen mode Exit fullscreen mode

🧠 So what’s the difference: try–catch with throw vs. without?

Let’s compare:

🥊 With throw inside catch:
You're passing the error up — often used when:
You can’t fix the issue here
You want to inform the caller that something went wrong

try {
    riskyThing();
}
catch (Exception ex) {
    Console.WriteLine("Logging the error...");
    throw;  // re-throws the same exception
}
Enter fullscreen mode Exit fullscreen mode

🪵 Without throw:
You're handling it locally — like logging, fallback values, etc.

try {
    riskyThing();
}
catch (Exception ex) {
    Console.WriteLine("Handled and moving on.");
    // No throw here
}

Enter fullscreen mode Exit fullscreen mode

🧾 Isn’t this just like if–else?**

Good instinct! Here's the key difference:

Feature ifelse trycatch
Usage For things you expect For things you don’t expect
Performance Faster (no overhead) Slower (exception handling has a cost)
Example case if (fileExists) try { read file } catch (error)
Best for Normal decisions Unpredictable errors

🚦 Example:
You can check if a file exists before opening it using if. But if the file vanishes between check and open — you'll need try–catch.

What happens if you throw but there’s no catch anywhere?
👉 If you throw and there's no matching catch anywhere in the call stack…
🧨 Your program crashes (or terminates the thread).

Example:

void Main() {
    SayHello();
}

void SayHello() {
    ThrowError();
}

void ThrowError() {
    throw new Exception("Boom!");
}
Enter fullscreen mode Exit fullscreen mode

There is no try–catch anywhere.
So when ThrowError() throws, it:

Looks in ThrowError() → ❌ no catch
Goes to SayHello() → ❌ no catch
Goes to Main() → ❌ no catch
Hits the top level — 💥 Unhandled Exception

⛓️ Think of it like this:
A throw is a message in a bottle.
Catch blocks are the lifeguards.
If nobody catches the message, it floats to the top and sinks the ship.

🧠 Why it matters:
Sometimes, you want that crash (like in tests).
But in real apps, especially APIs or games, unhandled exceptions are dangerous — they can:
Show ugly errors to users
Kill threads (especially bad in multithreaded apps)
Bring down whole services if not contained

Hope this was useful.
Next time, don't Try Catch me if you can with bugs ;)
If you havent watched that movie - go watch it first.

Top comments (0)