Guide | Flutter

How to Throw Exceptions in Flutter

How to Throw Exceptions in Flutter

An exception is an unexpected issue that occurs when executing a program and disrupts its normal flow. Exceptions can cause the application to terminate abnormally if not handled.

Exceptions are intended to be caught and handled so they do not affect the flow of the program. To handle an exception in code, try..catch..finally blocks should be used.


Why Throw Exceptions in Flutter?

Exceptions can be thrown to convey information to the user about a failure that occurred during the execution of a program. They should contain useful data fields to convey the relevant information. This can help the issue be addressed programmatically.

Exceptions can be instantiated and thrown in Flutter using the throw keyword. The syntax for throwing an exception is:

throw new exception_name()


Flutter Throw Exception Example

Here’s an example that shows how to use the throw keyword in Flutter to throw an exception:

void validate_age(int age) { 
    if(age < 0) { 
        throw new FormatException(); 

In the above example, the validate_age function is used to validate an integer age, which should not be negative in value. If the value is found to be less than 0, a FormatException is thrown.


Handling Exceptions in Flutter

To handle exceptions in Flutter, try..catch..finally blocks can be used to prevent the application from terminating abruptly.

The try block contains the code that might possibly throw an exception. The try block must be followed by on or catch blocks, and an optional finally block.

The catch block is used to catch and handle any exceptions thrown in the try block. To catch specific exceptions, the on keyword can be used instead of catch. The catch block can be left at the bottom to catch other exceptions.

The finally block is optional and is always executed whether any exception is thrown or not. It is executed after the try/on/catch blocks.

Here's the syntax for using the try..catch..finally blocks to handle an exception

try {
// do something
} on Exception1 {
    // handle exception
} catch (ex2) {
    // handle exception
} finally {
// always executed


Flutter Handle Exception Example

Here’s an example that shows how to use the try..catch..finally blocks in Flutter to handle an exception:

void main() {
    int a = 1;
    int b = 0;

    try {
        int result = a ~/ b;
    } catch (ex) {
        print('cannot divide by 0');
    } finally { 
        print('finally block executed'); 

In the above example, an exception is thrown due to a division by zero attempt. This exception is caught and handled by the catch block. The finally block is executed after the catch block and the following output is produced:

cannot divide by 0
finally block executed


Track, Analyze and Manage Errors With Rollbar

Managing errors and exceptions in your code is challenging. It can make deploying production code an unnerving experience. Being able to track, analyze, and manage errors in real-time can help you to proceed with more confidence. Rollbar automates error monitoring and triaging, making fixing Flutter errors easier than ever. Sign Up Today!