If some code returns a future and determines that the future should return "Error" or "Exception" how can a stack trace be passed to Completer.completeException(exception, stackTrace);
Asked
Active
Viewed 3.8k times
95

Günter Zöchbauer
- 623,577
- 216
- 2,003
- 1,567

adam-singer
- 4,489
- 4
- 21
- 25
2 Answers
188
If I understand correctly: when you catch an exception in dart, you can also catch the stack trace:
try {
// something
} catch(e, stacktrace) {
myCompleter.completeException(e, stacktrace);
}

Günter Zöchbauer
- 623,577
- 216
- 2,003
- 1,567

John Evans
- 6,858
- 4
- 28
- 26
-
2To add to this, if you don't have a current exception, just throw one in the try block and catch it yourself. – Justin Fagnani Dec 20 '12 at 03:13
-
1Hey, Adam, that sounds like a pretty good answer. You might want to green checkmark it :) – Shannon -jj Behrens Dec 20 '12 at 19:51
-
2John's answer is correct, the answer I was looking for is Justin's. – adam-singer Dec 20 '12 at 22:27
-
1@JustinFagnani why don't you go ahead and make a separate answer since it was closer to what Adam was looking for. :) – John Evans Dec 20 '12 at 22:58
-
Where do we get ```myCompleter``` from? – Oliver Dixon Nov 10 '22 at 10:26
173
If you're not in a catch block, you can use StackTrace.current

cambunctious
- 8,391
- 5
- 34
- 53
-
15This is useful if you need to get a stack trace from any arbitrary location, but if you're within a `catch` block, the chosen answer is better since Dart already provides a `StackTrace` to you. – Abion47 Jan 29 '19 at 21:26