DEV Community

loading...
Cover image for The fight between Try-Catch and If-Else

The fight between Try-Catch and If-Else

mrsaeeddev profile image Saeed Ahmad ・1 min read

How do you decide where to use if-else and try-except?

I have started this thread because many new people overuse try-catch or try-except over if-else or vice versa.

Share your thoughts!!!

Discussion

pic
Editor guide
Collapse
aligoren profile image
Ali GOREN

Imho, try-catch blocks are designed for exceptional cases, on the other hand, if-else blocks are designed for program logic.

There is no control rule in the try-catch specification for example;

try(age != null)

And you can't catch unexpected exceptions in the if blocks.

Collapse
rolfstreefkerk profile image
Rolf Streefkerk

They're functionally not even the same thing.
A try catch block is used to capture exceptions happening within that block.

If else is a control statement block, meaning, a certain variable has a certain state and you control the program flow based on that.

Collapse
endy_tj profile image
Endy Tjahjono

I add a lot of validations using if. If I can't use if, for example using a function that may throw error, I will add try catch only if I want to do something when an exception is thrown. If there is nothing to do when an exception is thrown, I will let the exception bubble up. I have application level exception handling to log the error.

I avoid using try catch to control program flow because it is not easy to spot that a codeblock will emit an exception: you have to read the whole codeblock to find places that emit exception.

Collapse
sfiquet profile image
Sylvie Fiquet

I don't see how you could use them interchangeably. If you're calling a function that generates exceptions, you should use try-catch (or decide that you'll let your program exit because it can't recover from that exception). If the function doesn't generate an exception but returns an error value then you use if. You don't have a choice.

The only question is when you design your own functions. Should this new function raise an exception when it fails or should it return an error? That's up to you. How bad is the failure? Does it need to interrupt the current task for special handling or can everything be handled easily by returning an error value? There's a performance cost that comes with exception handling so I'd say only use it when you can't do what you need with a return value.

Keep in mind that best practice for error handling might differ depending on the language you're using.

Collapse
moopet profile image
Ben Sinclair

In certain systems - terrible, heartbreakingly-awful systems - I use try.. catch instead of ifs, against those system's best practices.

If your ifs need a dozen conditions to get a single value out of an object, and you can do the same by handling an exception... go with the exception.

It's more readable, for one thing.

I know it makes it harder to track down what went wrong when something goes wrong. I know it pongs a bit.

But life's too short for entity metadata wrappers.

Collapse
jmfayard profile image
Jean-Michel Fayard πŸ‡«πŸ‡·πŸ‡©πŸ‡ͺπŸ‡¬πŸ‡§πŸ‡ͺπŸ‡ΈπŸ‡¨πŸ‡΄

I consider Try/Catch harmful most of the time.
Try/Catch is basically GOTO, and has pretty much the same drawback.
A good try/catch looks often not really different from a bad try/catch.

I am speaking here about normal logic here, not about "Oh god, I cannot access the hard-drive anymore, I'm dying"

Collapse
adam_cyclones profile image
Adam Crockett

You must know what your program should do, and that is possible to a couple of intentions, this is all covered by if else. But, your program might want to do something you didn't anticipate, this is where error catching comes in. Although this isn't the hardest question, should you reroute an error back to expected behaviour? Should you let it fall over in a blanket ah snap, or let it fall over and record a specific error, how should you log and when... It gets quite complex very quickly.

Collapse
cereal84 profile image
Alessandro Pischedda

Mainly try-catch has use when the code (i.e. libraries) launch them because some "exceptional cases" so you're forced to catch and handle them.
In addition to this case the use of if-then or try-catch depends on your code and how do you handle things (errors, special cases, etc).
If it is a new project you can decide how to handle this, or if you're maintaining code of some one obviously you'll try to use it's way to code in order to keep the code style consistent.

If-else is, usually, used to handle logic into the code flow without wrap every 2 line of code.

Anyway it is up to you decide the style.

Collapse
waylonwalker profile image
Waylon Walker

Its a matter of look before you leap vs asking for forgiveness. Some languages like python read better and can be more idiomatic using try-except over if-else.

Collapse
mrsaeeddev profile image
Saeed Ahmad Author

Yes. Actually, I was coming from JS so, it was quite confusing in the early days.

Collapse
mquanit profile image
Mohammad Quanit

try-catch for asynchronous operations and if-else for non-async operations.
I usually use try-catch for async-await which makes code clean and precise instead of promises.