Updated 3 May 2024
In this blog we will learn about Exception Handling in Flutter. An exception is an abnormal condition or event that occurs during the execution of a program and disrupts the normal flow of code.
Exceptions are an error which indicates that something unexpected happened in code.
Flutter throw exception when a special and unwanted circumstance occurs, such as trying to divide a number by zero, accessing a null reference, formate exception, network connection failures etc.
The program may be terminated if the exception is not handled properly.
You may also check our Flutter App Development services.
a) FormatException: Occurs when you try to parse a value in an incorrect format, like parsing a non- numeric string as an integer.
b) RangeError: Raised when an index or value is out of a valid range, like accessing an index that is out of bounds in a list.
a) PlatForm Exception: Often encountered when dealing with platform-specific code, like making native platform calls. It may include errors related to device features, such as camera access, permissions, or location etc.
In flutter you can create your own exception by defining new exception classes that extend existing Dart exception classes, such as ‘Exception‘ or ‘Error‘. It is helpful to can help you categorize and handle specific errors in your application more effectively.
Exceptions that can occur when we are working with asynchronous, like Future
and async/await
. These might include errors related to network requests, database operations, or timeouts etc.
When we dealing with network requests or asynchronous operations, you often use async/await
and handle exceptions with try-catch
blocks. Dart’s Future
and async/await
provide a structured way to handle asynchronous errors.
a) StackOverflowError: Occurs when the call stack becomes full.
b) OutOfMemoryError: Happens when the system runs out of memory.
a) FormatException: Occurs when you try to convert a string into a numeric type (int or double), and the string is not in the expected format.
b) RangeError: Thrown when you try to access an index or value that is out of a valid range, such as trying to access an index that is out of bounds in a list.
c) TimeoutException: This exception occurs when an operation takes longer to complete than a specified time limit, such as in network requests or asynchronous operations.
We can handle exceptions with multiple ways which is mentioned below.
We can handle the exception with help of Try/Catch block.
A try block is the block of code in which exception can occur. In try block you can put those code on in which exception can occur. during the execution of program.
Catch block is use to catch and handle any exceptions thrown in the try block. To catch specify exception with the help of catch keyword.
Finally block is optional and is always executed whether any exception is occur or not. Finally block is executed after the try/catch block .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
Future<void> youMethod() async { var url = Uri.parse('https://jsonplaceholder.typicode.com/posts'); try { final response = await http.get(url).timeout(Duration(seconds: 5)); if (response.statusCode == 200) { print("You response data is here==->${response.body}"); } else { print("Not Getting success"); } }catch (error, _) { print(error.toString()); }finally { print('API call completed.'); } } |
Exception handling in Flutter is necessary to make your app better, user-friendly, and easier to manage. Exception Handling is an essential component of building a reliable and flexible Flutter application.
For more knowledge related to this topic you can check here.
In this blog we have discussed about Exception Handling in Flutter.
You can also check other blogs from here for more knowledge.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.