What is Dart error handling?
Error handling in Dart involves handling exceptions and errors when the program encounters an unexpected situation.
This is crucial to make the program robust and user-friendly.
Dart uses 'try-catch' blocks to catch exceptions, with the 'on' keyword for specific types and 'catch' for all types.
What is Exceptions?
Whenever program terminate abnormally , it's time it's called exceptions , and handle this Exceptions we called exception handling.
Types of Exceptions
DefferedLoadException
- It is thrown when a deferred library fails to load.
FormatException
- It is the exception that is thrown when a string or some other data does not have an expected format
IntegerDivisionByZeroException
- It is thrown when the number is divided by zero.
IOEException
- It is the base class of input-output-related exceptions.
IsolateSpawnException
- It is thrown when an isolated cannot be created.
OSError
- it is thrown when operating system error.
Timeout
- It is thrown when a scheduled timeout happens while waiting for an async result.
Example
import 'dart:async';
void main()
{
String userInput = "3,14";
try{
double num = double.parse(userInput);
print("Squre value is $userInput * $userInput");
}on FormatException catch (e){
print("You have inavalid input , Enter valid value and try agian");
print(e);
}on IntegerDivisionByZeroException catch (e){
print("You have inavalid input , Enter valid value and try agian");
print(e);
}on DeferredLoadException catch (e){
print("You have inavalid input , Enter valid value and try agian");
print(e);
}
catch(e)
{
print("Somthing wrong happend in your output");
print(e);
}
}
Β