dartformatexception

throw custom FormatException using "throw" and try to handle it by try on . Dart Language


I tried to throw a custom FormatException by me and try to handle by using try on

try {
   print (' Enter Your age ') ; 
throw FormatException ;
}
on FormatException {
  print('Stop you enter invalid value  ') ;

}

output : didn't handle the exception ? i try using catch(e) and worked fine !

My Question is Why on FormatException won't handle the exception ??

the syntax of tryOn i wrote is true


Solution

  • throw FormatException throws the FormatException type (i.e, an object of type Type), but on FormatException expects to catch an instance a FormatException.

    Use throw FormatException() instead (optionally with an error string).