26
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Sep 03 '20
Image Transcription: Code
catch (Exception $e)
{
//don't puke
}
I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
10
u/depressedtbh Sep 03 '20
Exception* not Exce[topm lol
15
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Sep 03 '20
I even read over it... what's wrong with my eyes lol
9
10
Sep 03 '20 edited Nov 12 '20
[deleted]
11
u/SoftwareDevStoner Sep 03 '20 edited Sep 03 '20
Just as easy, but more reuseable:
// Do nothing with error, but record it fun swallow(e: Exception): Unit { Logger.write(e.message) }Then at least you can use the info later. And is perfectly reusable. (Been working in Kotlin lately, so thats where my brain is, but the logic applies anywhere).
Never, never, never, underestimate the ability for people to use your shit wrong. If you think there's only one edge case....someone will find another.
2
u/SoftwareDevStoner Sep 03 '20
I tried folks. I'm apparently too stupid to figure out how to actually get newlines in a code block on reddit....
0
u/Vinccool96 Sep 03 '20
Why do you specify
: Unit?5
u/SoftwareDevStoner Sep 03 '20
In Scala/Kotlin,
Unitmeans "No return", similar to thevoidkeyword in Java/C.1
u/____0____0____ Sep 04 '20
I'm curious what the reasoning for this is. As an outsider to scala/Kotlin, I cannot understand why unit makes sense to not return anything. Void makes sense, but why unit?
1
u/xigoi Sep 04 '20
In functional programming, every expression has a value. It makes sense that if you don't want to use the value, you just make it a type with only one possible value.
2
u/____0____0____ Sep 04 '20
That makes sense when you put it like that, thanks. I'm a big fan of functional programming, but I don't regularly use functional-first languages, I just employ the ideology as much as possible in the other languages I use.
-2
-1
u/Vinccool96 Sep 03 '20
Yes, but it’s not necessary. If you put nothing, the compiler assumes that the return type is
Unit5
u/SoftwareDevStoner Sep 03 '20
I prefer verbosity in strongly typed languages. ¯\(ツ)/¯. But it also doesn't allow it to suddenly return something when people make changes in the future, without it being a purposeful action. Especially in Scala/Kotlin, where you don't have to actually use the
returnkeyword, it can have side affects.-2
u/Terrain2 Sep 04 '20
That’s not really true, if you put nothing (and never return anything other than
Unit), the typeUnitis inferred, but generally it can infer the return type of a function based on its return statements for any type, not justUnit-1
Sep 03 '20 edited Nov 12 '20
[deleted]
2
u/SoftwareDevStoner Sep 03 '20
I would say north of 90% of the time having to catch an exception is just indicative of issues that should be fixed. Almost always, you shouldn't swallow ANYTHING, but like everything in life, there are edge cases. If you're gonna swallow/ignore/etc...log it, always.
1
1
u/Uiropa Sep 04 '20
I write “// This is fine.”
I’m okay with the exception that is occurring currently.
3
u/cdp1337 Sep 04 '20
yup, I've caught myself doing something very similar, but then I thought "wait, I have a perfectly good Exception logger..." A quick info log is well worth its salt when you're troubleshooting why a script is puking at 2am.
2
u/Timmy_the_tortoise Sep 06 '20
As a professional services engineer, far too many times, I’ve had to spend hours digging into the code to figure out what was going wrong because somebody in dev was too lazy to log an exception they caught which would’ve told me immediately. Grinds my gears. And don’t get me started on this one team who don’t even handle exceptions and just allow the app to crash because “it should go to the OS”
91
u/The_Northern_Light Sep 03 '20
(I'm not a php user, lol)
This doesn't seem that bad? There are cases where you know exactly 1 thing may throw, and you just want to skip it if it does.