Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way in Dart to mark a function as throwing an exception?

I was trying to find a way in Flutter/Dart to mark a function that may throw an exception during its execution. After some time searching in the documentation and Google I did not find any way of doing this.

In other language, for example Swift, Java, Kotlin, etc I know we have such mechanism. Sample code in Swift is:

func doSomething() throws { ... }

Does anyone know if this exists in Dart? I think it will be useful.

If it does not exist due to Dart language desing then maybe anyone can explain the reason behind this decision.

Thanks in advance!

like image 399
Gabi D Avatar asked Feb 13 '20 12:02

Gabi D


People also ask

How do you throw an exception in darts?

Throwing an exception As we have seen, when an illegal operation is performed, Dart throws an exception. If a user is trying to perform an illegal operation with your code and it is necessary to terminate a program with an exception, you can throw an exception manually using throw keyword.

What does Rethrow do in Flutter?

rethrow preserves the original stack trace of the exception. throw on the other hand resets the stack trace to the last thrown position. The biggest difference is the preservation of the original stack trace.

Which are the predefined types used to throw exception in Dart?

Every built-in exception in Dart comes under a pre-defined class named Exception. To prevent the program from exception we make use of try/on/catch blocks in Dart. Catch: Catch block is written with try block to catch the general exceptions: In other words, if it is not clear what kind of exception will be produced.

Does throwing an exception end the method?

When an exception is thrown the method stops execution right after the "throw" statement. Any statements following the "throw" statement are not executed.


1 Answers

There is no way in Dart to mark a function as potentially throwing.

All functions should be assumed to potentially throw (if for no other reason, then because of an out-of-memory or stack-overflow situation).

If you look at Swift, the throws is about exceptions, not errors. Dart does not distinguish the two, you can throw anything. Swift has put itself in a place between Java ("have to declare all thrown exceptions") and Dart or C# ("Can't declare exceptions").

Marking a function as "throwing" doesn't help the compiler in any way because it has to assume that all other functions might too. The Swift approach is there to ensure that distinctively marked exceptions are not ignored. Unless you want to, then you can try! them and turn the exception into an error.

If a function does throw as part of normal usage, you should document it in the function's documentation.

Dart also have the issue of function types. Is a function from int to int the same type as another function from int to int if the latter can throw? Separating function types into throwing and non-throwing get complicated quickly. Even more so if you want to specify what it throws. It's not impossible, but it's one more complication.

The one thing that you will get with the Dart null safety update (currently being worked on), is a way to state that a function always throws. If you make the return type Never in null-safe code, then the type system will prevent you from returning any value, and since a function call must end by either returning a value or throwing, a call to a function with return type Never can only end by throwing.

like image 170
lrn Avatar answered Oct 05 '22 19:10

lrn