Replies: 13 comments
-
@lachbaer Either |
Beta Was this translation helpful? Give feedback.
-
Your example still requires something like: catch (CommunicationException cex, SystemException sex) {
if (cex != null) {
//do this
}
else if (sex != null) {
// do that
}
} So how is that any more concise than catch (CommunicationException cex) {
// do this
}
catch (SystemException sex) {
// do that
} The only time yours is shorter is if you don't care which exception occurred which I would think is rare. By the way, I should add, C#7 already allows: catch (Exception ex) when (ex is CommunicationException cex || ex is SystemException sex) {
//handle it
} |
Beta Was this translation helpful? Give feedback.
-
@dman2306
In case I don't care about the details of the exception and just want to log it somehow. There is another discussion #220. Meanwhile I really wonder if I am so misled about exceptions?! 😞 As nearly every method, every constructor, etc. may throw exceptions, do really wrap every single call in a |
Beta Was this translation helpful? Give feedback.
-
I would argue your goal should be to prevent exceptions, not to handle them. Meaning, many methods throw By the way, from a logging perspective, I would still say "A Communication error occurred" or "A System error occurred" vs. "Either a communication or system error occurred" that the former example is much more useful. |
Beta Was this translation helpful? Give feedback.
-
Your example is so true. Sorry- couldn't resist. :) |
Beta Was this translation helpful? Give feedback.
-
@MgSam 😆 |
Beta Was this translation helpful? Give feedback.
-
@alrz I agree, this should be intersection types. Nitpick, they would both be definitely assigned. The proposal says they are set to null. |
Beta Was this translation helpful? Give feedback.
-
Borrowing from Java, using union type(or intersection type, what's the difference in .net?)- catch (CommunicationException|SystemException ex) {
//handle it ex.
} |
Beta Was this translation helpful? Give feedback.
-
I was just about to propose this, but the syntax I chose was slightly different:
This makes it somewhat more like a |
Beta Was this translation helpful? Give feedback.
-
Omitting the exception variable is currently valid and means that you are discarding the instance of the exception. It would be inconsistent for it to mean something else there. Also, you also run into local shadowing issues by having an implicit local injected into scope. For properties that's not an issue since the only parent scope is the fields of the class which you could always reference via |
Beta Was this translation helpful? Give feedback.
-
I like this: catch (ArgumentException ex)
catch (NullReferenceException ex)
{
// ex is best common type now, and one day an intersection type.
} |
Beta Was this translation helpful? Give feedback.
-
@jnm2 |
Beta Was this translation helpful? Give feedback.
-
Linking my union and intersection types proposal- #399 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Instead of
(what already is an easy compared to C# 5) it would be much lighter to write
There are times when I want to catch certain exceptions that I am particularly not further interested in. The code in the
catch
block only exists for common error handling.Beta Was this translation helpful? Give feedback.
All reactions