You can throw any valid object in Dart and still get a stack trace. Note that the stacktrace is independent from the error/exception.
void main(List<String> arguments) {
try {
throw "I'm throwing a string, not an error!";
} catch (e, stack) {
print(
"error: $e",
);
print("stack: $stack");
}
}
╭─iamwill@zues ~/Desktop/test
╰─$ dart run 255 ↵
error: I'm throwing a string, not an error!
stack: #0 main (file:///Users/iamwill/Desktop/test/bin/test.dart:3:5)
#1 _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:281:32)
#2 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)
void main(List<String> arguments) {
try {
throw false;
} catch (e, stack) {
print(
"error: $e",
);
print("stack: $stack");
}
}
╭─iamwill@zues ~/Desktop/test
╰─$ dart run
error: false
stack: #0 main (file:///Users/iamwill/Desktop/test/bin/test.dart:3:5)
#1 _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:281:32)
#2 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)
void main(List<String> arguments) {
try {
throw 8;
} catch (e, stack) {
print(
"error: $e",
);
print("stack: $stack");
}
}
╭─iamwill@zues ~/Desktop/test
╰─$ dart run
error: 8
stack: #0 main (file:///Users/iamwill/Desktop/test/bin/test.dart:3:5)
#1 _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:281:32)
#2 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)