[Proposal]: Equivalent to "throws" keyword #4059
-
Equivalent to throws keyword
SummaryIn C# you do not really know whether can method throw exception or not. All you can do now is use method naming conventions (btw, I did not found any) like in dynamic languages such as Ruby. Which is a shame for such beautiful, static language as C#. What I suggest is to adopt Java's MotivationThis will help to reduce errors in runtime. Detailed designIn Java if method can throw exception you must either catch it or specify, that this method can throw exception of some type, even if you do not use this method at all. class Main {
public static void test() throws Exception {
if (false) {
throw new Exception();
}
}
public static void main(String[] args) { }
} It can be optional or mandatory. Suggested C# syntax: class Program
{
static void test() ! Exception
{
throw new Exception();
}
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
} DrawbacksAlternativesUnresolved questionsDesign meetings |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
C# very intentionally did not adopt java's checked exceptions, which are also widely hated by Java developers - both scala and kotlin don't have them despite being made for the JVM for example. I think you'll have to explain why that decision should be revisited, or come up with a design that avoids many of the pitfalls of Java's design for this to be considered. A much more promising direction is to take a leaf out of functional languages, and introduce a |
Beta Was this translation helpful? Give feedback.
-
See here for why C# doesn't have checked exceptions https://www.artima.com/intv/handcuffs.html |
Beta Was this translation helpful? Give feedback.
-
There's an |
Beta Was this translation helpful? Give feedback.
See here for why C# doesn't have checked exceptions https://www.artima.com/intv/handcuffs.html