What if your #flutter app fails? Should you throw? Error? Exception?

#dart gives you both Error and Exception 🎯

But how do they work? Which one should you choose? πŸ€”

Here is the answer πŸ‘‡πŸ§΅
Error & Exception ‼️

#dart has both of them, but they are not the same πŸ’πŸΌβ€β™‚οΈ

πŸ‘‰ Exceptions: conditions that you can plan ahead for *and catch*

πŸ‘‰ Errors: conditions that *you don’t expect*
Exceptions should be handled in your code πŸ’»

You (developer πŸ’πŸΌβ€β™‚οΈ) know that they can happen, and so you plan accordingly

πŸ‘‰ *It is intended to be caught*

πŸ‘‰ It should contain useful data fields

πŸ‘‰ Should not crash the app
Is good practice to define a custom exception by implementing the Exception interface

Give it meaningful information and a readable message πŸ’‘
#dart has a good set of tools for Exceptions

β˜‘οΈ try
β˜‘οΈ catch
β˜‘οΈ catch (on)
β˜‘οΈ StackTrack
β˜‘οΈ finally
β˜‘οΈ rethrow
Example of Exceptions

πŸ‘‰ FormatException

πŸ‘‰ IOException

πŸ‘‰ TimeoutException
Errors represents a program failure πŸ’₯

These are not expected

If they occur, the program is erroneous, and terminating the program may be the safest response πŸ’†β€β™‚οΈ
Example: `String.contains`

‼️ `startIndex` must not be negative or greater than `length`

There is clearly an error in your program if you call `'ABC'.contains('A', -10)` πŸ’πŸΌβ€β™‚οΈ
Note ℹ️

Error objects are also used for system wide failures like stack overflow or an out-of-memory situation
Example of Errors

πŸ‘‰ NoSuchMethodError

πŸ‘‰ StackOverflowError

πŸ‘‰ UnimplementedError
There is a lot behind all of these, here the docs for Exceptions πŸ“ƒ

dart.dev/guides/languag…
Error handling is hard but absolutely crucial for any app

And there is much more to discover πŸ—Ί

Follow @SandroMaglione (me πŸ‘‹) for more about #dart and #flutter

β€’ β€’ β€’

Missing some Tweet in this thread? You can try to force a refresh
γ€€

Keep Current with Sandro Maglione πŸ’»

Sandro Maglione πŸ’» Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @SandroMaglione

Oct 18
Error handling is hard, harder than you think πŸ€”

Let's just see a simple example πŸ’πŸΌβ€β™‚οΈ

In how many ways can this #dart code below fail?

The answer will surprise you πŸ‘‡πŸ§΅ Image
Note: fail can mean different things ☝️

Here we mean anything that will crash the app (if not handled)

Errors or Exceptions πŸ‘‡

And the answer is... πŸ”Ÿ

Yes, 10 ways 🀯

Basically every function hides a potential issue

What? Well, let's see them all πŸ‘‡
Read 15 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(