Skip to main content

avoid-throw-objects-without-tostring

added in: 1.4.0
Pro+
preset: recommended

Warns when a thrown object does not implement toString.

Objects that do not implement the toString method will be represented as "Instance of ..." when serialized.

Example

❌ Bad:

void function() {
try {
// ...
} on Object {
throw MyException(); // LINT: Avoid throwing objects that do not implement the 'toString' method.
}
}

class MyException {}

✅ Good:

void function() {
try {
// ...
} on Object {
throw MyException();
}
}

class MyException {

String toString() => ...;
}

Additional Resources