UNDERSTANDING ERRORS AND EXCEPTIONS IN FLUTTER

XROLE DIAMOND
4 min readSep 20, 2021

--

One of the things that cause application crash is not handling exceptions or poor exception handling. Application crashes results in poor user experience and can cause users never to use the app again. As software engineers, it is our duty to handle these exceptions, hence, in this article, we will be covering;

i. What are Exceptions

ii. Types of Exceptions

iii. Error

iii. Difference between errors and exceptions

iv. How to handle exceptions to prevent application crashes and notify users of what is happening.

Note: This article will be divided into two (2) parts in other not to bore readers with too much text.

WHAT IS EXCEPTION

The term exception is shortened from the phrase “exceptional event”. An exceptional event is a problem that occurs during the execution of the program. When an Exception occurs the normal flow of the program is disrupted and the program/Application terminates abnormally. When there is a failure within a method, the method creates an object and hands it off to the runtime system. This object, called an exception object, contains information about the failure, including its type and state of the program when the failure occurred. This process of creating an exception object and passing it to the runtime system is called “throwing an exception”. One thing to note here is that this happens mostly at runtime and the cause of the failure does not come from the engineer as it could be no internet connection, poor internet, invalid data type getting returned from the API call, etc.

TYPES OF EXCEPTIONS

Because there are numerous types of exceptions and because you do not need to handle all exceptions separately, knowledge of these exceptions are required so that we can handle them accordingly. In flutter/Dart, below are some of the exceptions users might face.

  1. SocketException: This happens when a user device is not connected to the internet
  2. Timeout: Thrown when a scheduled timeout happens while waiting for an async result. This is largely because of poor internet connection.
  3. FormatException: Exception thrown when a string or some other data does not have an expected format and cannot be parsed or processed.
  4. DefferedLoadException: Thrown when a deferred library fails to load.
  5. IntegerDivisionByZeroException: Thrown when a number is divided by zero.
  6. IsolateSpawnException: Thrown when an isolate cannot be created.

WHAT IS ERROR

According to dart, an error represents a program failure that an engineer should have avoided. For example,

calling a function with invalid number of arguments

accessing a list with an invalid array index, etc.

There is really a thin line between error and exception, however, to differentiate these;

1. Error comes from the programmers while exception comes from an external source.

2. Errors are avoidable while exceptions are not but can be handled.

3. Errors happen mostly at compile time while exceptions happen at runtime.

Just like I pointed out before now, these exceptions come with an exception object that contains a message to let the developer know what is going on. As developers, when handling these exceptions, we do not want to show users these low-level exception messages, we need a way to format these messages to what the users will understand (i.e user-friendly message) and

show it to the users. In part two (2) of this article, we will be looking at how to handle these exceptions and convert what might be a potential threat to our application to a greater user experience.

Thanks for sticking to the end, see you in part two.

--

--