Home > Software engineering >  Is there any practical difference between throw Exception('message') vs throw 'messag
Is there any practical difference between throw Exception('message') vs throw 'messag

Time:05-27

Is there any practical difference between using:

throw Exception('message');

vs

throw 'message';

When I want to throw an error in flutter?

CodePudding user response:

throw 'message' throws a String.

Callers trying to catch it either would need to do:

try {
   throw 'message';
} on String catch (s) {
  ...
}

or would need to use a blanket, untyped catch clause (which isn't recommended). Doing this would be very unusual and would not be behavior callers would expect. There is an only_throw_errors lint to warn about this.

throw Exception('message'); throws a constructed Exception object. That's far more typical.

  • Related