✅ Creditable usage for `goto`?
I don't think it needs further explanation, basically the goal is to catch any exceptions that might be thrown from inner functions that get called, but to also allow your own exceptions to be thrown when validation fails. Any thoughts?
A)
B)
C)
36 Replies
Unknown User•3w ago
Message Not Public
Sign In & Join Server To View
Wat? No, custom exceptions are great
I'd go with A. Yes B's more "clever" and takes fewer lines, but A's more consistent with what people are used to seeing
hold on, I totally forgot about the
when clauseI think
when would make it harder to read
Like so?
Unknown User•3w ago
Message Not Public
Sign In & Join Server To View
Pretty legit imo
but what if there are more exceptions to be taken care of
I like
when for taking logic out of the body of the catch, but not for removing another catch altogetherUnknown User•3w ago
Message Not Public
Sign In & Join Server To View
This is hardy "complex things". This is bread-and-butter error handling
I added yours to the post
Unknown User•3w ago
Message Not Public
Sign In & Join Server To View
catch (Exception e) when (e is not MyException1 and not MyException2 and not MyException3)
:harold:Use base classes, properties, etc.
catch (ValidationExceptionBase)yup that does seem to solve most of the problems
catch (ValidationExceptionBase e) when (e.IsRecoverable) or something etcbut in my real case, the other exceptions that need to be caught already inherit from the same base, and some of them I cannot even edit/change (they are not from my code base)
catch (Exception e) when (e is IValidationException)anything but goto? :catfine:
It's not that goto is always horrible, it's that people aren't used to seeing it used, and don't know how to easily spot the common usage patterns
Code should be obviously correct
right
is there no permission to open a poll or something?
Unknown User•3w ago
Message Not Public
Sign In & Join Server To View
:catderp: not really what I wanted
but obviously the duck
Unknown User•3w ago
Message Not Public
Sign In & Join Server To View
or use a base type / interface / etc., or do
catch (Exception e) when (e is not (MyException1 or MyException2 or MyException3))I now realize it is not a duck but something called "dodo"
think I'm gonna go with the latter option too in the end
Unknown User•3w ago
Message Not Public
Sign In & Join Server To View
If you have no further questions, please use /close to mark the forum thread as answered
I'll wait a couple hours in case somebody new hops in, gonna close it dw
i mean, my real opinion is a) don't throw exceptions you didn't need to start with, b) catching all exceptions & discarding like that is quite sus, c) i don't mind goto (but not convinced it's really needed here tbh)
It's no different to calling a helper method which throws the exception
not that this really matters - but i mean, it is different - you wouldn't say if/goto is no different to an if/else, even when you can get the same IL from it
I mean, putting the whole body of the
try (or some significant subset) into a helper method
What if throwing the exception needs a bunch of local state, which is only available at the point that the exception is thrown?
Although, replace your enum with constructing (but not throwing) the exception, and that can work well
I mean, if you had a method which called a bunch of other methods in a try, you wouldn't complain about one of those methods throwing an exception and you catching it. Inlining those methods directly into your try should make no differencethen I'd say: for a very short and simple case, C is better; but if some additional steps need to be executed for each exception type, then B. Snippet A looks awkward to me too (throw X -> catch X -> throw)
?
i dont really think a single big try-catch box is good fit, cconsider:
which is the same as
I see what you mean
splitting it into two try-catch blocks also solves the issue in my case
thanks for all of your suggestions :)