• Guides
    • Getting started with Dart
  • Blog
  • FAQ
    • Dart
    • Flutter
  • Contact
  • Guides
    • Getting started with Dart
  • Blog
  • FAQ
    • Dart
    • Flutter
  • Contact
  • Getting Started with Dart
    • Dart Variables
    • Dart Operators
    • Dart Comments
  • Dart Data Types
    • Dart Strings
    • Dart Numbers
    • Dart Booleans
    • Dart Lists
    • Dart Maps
  • Control Flow
    • Dart if...else Statements
    • Dart switch Statement
    • Dart for Loop
    • Dart for...in Loop
    • Dart while and do...while Loops
    • break and continue Statements
  • Dart Functions
  • Dart Classes
  • Dart Objects
  • Dart Constructors
  • Dart Exceptions

Dart Exceptions

107 views 1

Written by Ajaya Shrestha
January 24, 2022

In this chapter, we will learn about the dart exceptions and why it is important to handle those exceptions.


Dart Exceptions

An exception is an issue that may occur when we run the program. Exception is a run-time error.

When an exception occurs, the program terminates. An example of an exception is when we try to divide a number by zero.


Types of Dart Exceptions

Here are the built-in dart exceptions:

ExceptionDescription
DefferedLoadExceptionThrown when a deferred library fails to load
FromatExceptionException thrown when a string or some other data does not have an expected format and cannot be parsed or processed
IntegerDivisionByZeroExceptionThrown when a number is divided by zero
IOEExceptionBase class for all Inupt-Output related exceptions
IsolateSpawnExceptionThrown when an isolate cannot be created
TimeoutThrown when a scheduled timeout happens while waiting for an async result

Handling Exceptions

It is very important to handle and manage exceptions. Dart provides various methods to handle those exceptions.

Some of them are:

The try / on / catch Blocks

The syntax for handling an exception is:

try { 
   // code that might throw an exception 
}  
on Exception1 { 
   // code for handling exception 
}  
catch Exception2 { 
   // code for handling exception 
}

The block of code that might throw an exception is used inside the try block.

The on block helps in specifying the exceptions.

The catch block helps to handle the error that occurs in the try block.


Example: try with catch Block

void main() {   
   int a = 20;   
   int b = 0;   
   int result;    
     
   try {    
      result = a ~/ b;
      print(result);
   }    

  catch(e) {   
      print("Error");
      print(e);
   }   
}

Output

Error
Unsupported operation: Result of truncating division is Infinity: 12 ~/ 0

In the above example, we have tried to divide the integer value 20 with 0. This results in an exception.

When the exception occurs, the program flow moves to the catch block.


The Finally Block

The syntax of finally block is:

try {   
   // code that may be throw an exception   
}    
on Exception1 {   
   // exception handling code or specifying the exception  
}    
catch Exception2 {   
   //  code for exception handling   
}    
finally {   
   // code that should always execute; whether exception or not.  
}

The finally block always executes after the try/on/catch whether there is an exception or not.

Example: Finally Block with Exception

void main() {   
   int a = 20;   
   int b = 0;   
   int result;    
     
   try {    
      result = a ~/ b;
      print(result);
   }    

  catch(e) {   
      print("Error");
      print(e);
   }
  finally {
    print("Finally is executed always.");
  }
}

Output

Error
Unsupported operation: Result of truncating division is Infinity: 20 ~/ 0
Finally is executed always.

In the above example, an exception arises and that exception is handled by the catch block.

The code inside finally block is also executed.


Example: Finally block without Dart Exception

void main() {   
   int a = 20;   
   int b = 2;   
   int result;    
     
   try {    
      result = a ~/ b;
      print(result);
   }    

  catch(e) {   
      print("Error");
      print(e);
   }
  finally {
    print("Finally is executed always.");
  }
}

Output

10
Finally is executed always.

In the above example, there is no any exceptions. Also, the finally block is executed.

The finally block is useful when we want to execute a block of code in all conditions, whether there is a run-time error or not.

Was this helpful?

1 Yes  No
Related Articles
  • Dart Constructors
  • Dart Objects
  • Dart Classes
  • Control Flow
  • Dart Functions
  • break and continue Statements
Previously
Dart Constructors
flutter-dev-experts




FLUTTERGUIDE
FlutterGuide is an easy learning guide platform to help you build apps for any screen. The leading Dart/Flutter guides for getting started and become pro.
Guides
  • Getting started with Dart 22
  • Private Documentation 1
Categories
  • Firebase
  • Dart
  • iOS
  • API
  • Bloc
  • Flutter
  • Widgets
  • Mobile
  • State Management
  • Design
  • Animations
  • Flutter Designs
About
About Us
Guides
Blogs
FAQs
Contact
  • Privacy Policy
  • Terms & Conditions
  • Copyright 2022 Fawesome Apps. All Rights Reserved