Discussion: throw a string #1964
-
Exception classes are great, but sometimes in short scripts you want to just die with an error message. It gets a bit clunky to keep writing
when it would be more concise to just
When the value following |
Beta Was this translation helpful? Give feedback.
Replies: 18 comments
-
Generally speaking, throwing Visual Studio includes an explicit code analysis rule to this effect (CA2201):
Someone asked about this on Stack Overflow, where the top answer says:
I know that @epa, you're suggesting this for scripts (emphasis added):
However, the kind folks on the LDM have made it clear that they never want to go down the route of creating different dialects of C# for different uses - the one C# language has to handle all situations. This means there's no way to support this idea for scripting (where it might just possibly be defensible) without also permitting it for the vast body of code where it would have a lot of bug-inducing consequences. What I've done in situations like this is to create a helper method that makes things more concise: throw Error("It's dead, Jim");
private Exception Error(string message)
=> new InvalidOperationException(message); |
Beta Was this translation helpful? Give feedback.
-
I don't see how it would have bug-inducing consequences when you can already just I see it more along the lines of array initializer expressions, where C# has a useful shortcut for something that would otherwise require you to use a scripting language instead. (And there was a time when initializing arrays of data inside the program body was subject to a lot of moral disapproval, because obviously in a robust application you should separate code and data...) |
Beta Was this translation helpful? Give feedback.
-
Issue #538, Proposal: never/bottom type, offers a possible solution here, if it were ever implemented. You'd be able to write, eg a method like: Never Die(string reason) => throw new Exception(reason); and then use it in place of an explicit double Divide(double a, double b) => b != 0.0 ? a/b : Die("Cannot divide by zero"); |
Beta Was this translation helpful? Give feedback.
-
P.S. it would also be possible to define a specific exception class for this syntax, so that
Then you'd comply with the rules about not throwing In fact, it would be a small improvement in robustness, since people who currently have to type |
Beta Was this translation helpful? Give feedback.
-
If this is your goal, then define your own Exit function that does: Console.WriteLine(message);
Environment.Exit(exitCode); Then just call |
Beta Was this translation helpful? Give feedback.
-
I'm not sure that that would be much more robust. One real advantage of throwing explicit exceptions is how you can convey information about what went wrong simply by throwing an exception of a specific type. For example, if I encounter an In contrast, This just feels like it's going to promote the otherwise discouraged practice of throwing nonspecific exceptions. |
Beta Was this translation helpful? Give feedback.
-
Thanks for your comments. Of course I absolutely agree that parsing exception strings is horrible garbage. If you want to catch the exception and handle it, it's far cleaner to have different exception types. What I suspect some may be missing is that I'm talking explicitly about exceptions you don't want to catch. Most likely this is in a standalone program (not part of a long-running server process, and not with a GUI) where you check for various error conditions and just need to abort the whole process and print a stack trace. These are programs where you'd often reach for a scripting language such as Perl with its handy Another use is for 'impossible' conditions where if the |
Beta Was this translation helpful? Give feedback.
-
I think exceptions are a robust enough mechanism to convey the error that a programmer encountered. Additionally, you can ship analyzers that will warn/error programmers when they write code that you know would produce runtime errors. |
Beta Was this translation helpful? Give feedback.
-
Yes, they are! Much better than printing a message and calling |
Beta Was this translation helpful? Give feedback.
-
You can do this. I already showed how above :). The code i listed will do all you want, besides printing the stack trace. But if you want that, it's trivial for you to add it. I don't see wy you need any language features here since you could just just make this a trivial library function that you could then use from anywhere in your app. |
Beta Was this translation helpful? Give feedback.
-
Thanks CyrusNajmabadi. I should have realized that printing a stack trace is also something that can be done easily from the library function. |
Beta Was this translation helpful? Give feedback.
-
yup yup! :) |
Beta Was this translation helpful? Give feedback.
-
Being able to throw something other than an exception would be a nice way of implementing non-local control flow without sacrificing performance. |
Beta Was this translation helpful? Give feedback.
-
I support this. simplicity is justice. |
Beta Was this translation helpful? Give feedback.
-
Does this require a CLR change? I see mention that apparently a conforming runtime can allow throwing, and catching, non-exception based objects, but it seems to me that it isn't required by a CLS conforming runtime to support it? |
Beta Was this translation helpful? Give feedback.
-
@GeirGrusom Not inherently. The CLR already permits throwing any object type. When you use |
Beta Was this translation helpful? Give feedback.
-
I was going to suggest using an alias, but yours is even better. Really elegant solution. |
Beta Was this translation helpful? Give feedback.
-
It would be possible if we'll ever get something like extendable implicit operators.. The closest thing you can currently get to fulfill your desire for the least amount of keystrokes is probably something like this:
And then calling it like so:
This isn't really that useful though. What is useful, is extendable implicit operators, can we get those please? |
Beta Was this translation helpful? Give feedback.
Generally speaking, throwing
Exception
is considered poor practice because it prevents any/all consumers from doing anything specific with that one exception.Visual Studio includes an explicit code analysis rule to this effect (CA2201):
Someone asked about this on Stack Overflow, where the top answer says: