Skip to main content

Command Palette

Search for a command to run...

Dart Error Handling : Errors & Exceptions

Updated
2 min read
Dart Error Handling : Errors & Exceptions
V

"Hello World, I'm Vinit Mepani, a coding virtuoso driven by passion, fueled by curiosity, and always poised to conquer challenges. Picture me as a digital explorer, navigating through the vast realms of code, forever in pursuit of innovation.

In the enchanting kingdom of algorithms and syntax, I wield my keyboard as a magical wand, casting spells of logic and crafting solutions to digital enigmas. With each line of code, I embark on an odyssey of learning, embracing the ever-evolving landscape of technology.

Eager to decode the secrets of the programming universe, I see challenges not as obstacles but as thrilling quests, opportunities to push boundaries and uncover new dimensions in the realm of possibilities.

In this symphony of zeros and ones, I am Vinit Mepani, a coder by passion, an adventurer in the digital wilderness, and a seeker of knowledge in the enchanting world of code. Join me on this quest, and let's create digital wonders together!"

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

  1. DefferedLoadException

    • It is thrown when a deferred library fails to load.
  2. FormatException

    • It is the exception that is thrown when a string or some other data does not have an expected format
  3. IntegerDivisionByZeroException

    • It is thrown when the number is divided by zero.
  4. IOEException

    • It is the base class of input-output-related exceptions.
  5. IsolateSpawnException

    • It is thrown when an isolated cannot be created.
  6. OSError

    • it is thrown when operating system error.
  7. 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);
  }
}

More from this blog

Vinit Mepani (Flutter Developer)

270 posts

"Vinit Mepani, passionate coder! Dive into my Dart and Flutter journey on the blog. Let's master these tech wonders together. Happy coding! 🚀"