Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
asgs112754yI have the else directly under the "critical" code to execute in try block without else
-
But how is "else" different from "try"?
Also exceptions can be thrown in every block... Maybe going functional and using "Either" is the way to go? -
That's one inefficient way to replace a newline. It's like chaining Promises with then() but without Promises.
If you ask me, the best way to handle those things is how Rust does it - just put a `?` after the possibly failing operation and carry on. -
Try catch in general is utter bullshit. Look at C or even better Golang how it's done.
-
@iiii The ? operator is literally syntactic sugar for Rust's try-catch counterpart. And can be used within nested expressions, so it often saves quite a bit of boilerplate and indentations.
-
@iiii Not sure what you mean, but you can use it with any function call that can return an error or an optional result. The only condition is that the caller needs to be able to return the same error type, which is usually handled by defining a project-wide enum type that encompasses all errors and optionally casts errors from 3rd party libraries.
But this also means it cannot be used directly in the main() function as it doesn't return anything. -
try:
...
except:
...
else:
... dumb block!!!
finally:
...
No need for an else as you can catch exceptions of various types in most languages... And also the keyword 'else' is conditional and doesn't fit in that position. -
iiii90854y@deadlyRants I mean that it does not affect the next expression. Or is it not like that?
-
@iiii In case of an error it basically causes the function to immediately return with the error value, everything after that is not executed. It's a shortcut to pass errors back through the call hierarchy.
-
iiii90854y@deadlyRants so it basically makes the whole function a try block? Sounds like a bit of overcomplication to me.
-
Just return null for everything except success.
If given shit by anybody, say that's now the DEVELOPERS of LANGUAGES do it whilst pointing at PHP. -
iiii90854y@HiFiWiFiSciFi no, there may be the case when there may be an error but also 'null' being a valid value.
-
@iiii
try {
if(x==null) {
exit;
// It works!
}
catch {
exit;
}
It's just failing silently all the way down. -
iiii90854y
-
@iiii Yeah! It is kind of that.
Like more often than not I need to do some CRUD operation against some DB or API or other Datasource.
So I'll just have a "working" path, I guess this is called the "railway" and then everything else will fail silently or return null.
Then all that needs be done on the front-end is have a default value (or pass a default from the backend).
So either "were doing a CRUD operation that works" or we have a null so we need to do a CREATE instead of an UPDATE. Then you code the path for that based on the null... and you're STILL ON THE RAIL.
CHOO CHOO mutherfuckas -
@SuspiciousBug if there's an exception in the else block, it's not caught by the block above
Related Rants
There is normal to have an unexpected exception in a life.
If you learn to code, you know thats why there is try/catch exist, so when you try something and there is an exception you know how to pass it.
Why? So whenever something goes unexpected from your expectation you already have an exception for it and just pass it rather than making your entire mind crashing!
random
life lesson
try
exception handling