You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The ability to sum throw types for functions would make errors much more useful in Dart. In practice I see a lot of developers just not handle errors or go to great lengths to catch everything and rethrow them as an uber-error with a string enum and string message.
As I recall, Swift has the ability to sum error types, removing types that have been explicitly caught, and adding rethrow types in their place. This feature might look something like this in dart.
voidmain() {
try {
/// This would have a return type of [String] /// and a sum throw type of [BarException] or [BazException]final result =throwsBarBaz('');
} switch (e) {
caseBarException:print('bar exception!');
break;
caseBazException:print('baz exception!');
break;
}
}
/// Return type of [String]/// Throw type of [FooException] or [BarException]StringthrowsFooBar(String e) {
if (e =='foo')
throwFooException();
elseif (e =='bar')
throwBarException();
elsereturn e;
}
/// Return type of [String]/// Throw type of [BarException] or [BazException]StringthrowsBarBaz(String e) {
try {
final firstPass =throwsFooBar(e);
if (firstPass =='baz')
throwBazException();
elsereturn e;
} onFooException {
return'foo';
}
}
classFooException {}
classBarException {}
classBazException {}
The text was updated successfully, but these errors were encountered:
Is the request here a better syntax for catching different types, or to have some kind of static checking of thrown types like checked exceptions? If it's just the former, you can already have multiple on clauses:
voidmain() {
try {
/// This would have a return type of [String] /// and a sum throw type of [BarException] or [BazException]final result =throwsBarBaz('');
} onBarExceptioncatch (e) {
print('bar exception!');
} onBazExceptioncatch (e) {
print('baz exception!');
}
}
The current syntax is actually even shorter than your proposal. :)
If it's to have static checking of them, well there's #984. But that was closed because I think the world has generally decided that checked exceptions are a bad idea.
Related: #68 #349 #83
The ability to sum throw types for functions would make errors much more useful in Dart. In practice I see a lot of developers just not handle errors or go to great lengths to catch everything and rethrow them as an uber-error with a string enum and string message.
As I recall, Swift has the ability to sum error types, removing types that have been explicitly caught, and adding rethrow types in their place. This feature might look something like this in dart.
The text was updated successfully, but these errors were encountered: