2

Talking about garbage collected languages in general:

Will performance in term of cpu cycles decrease when an exception is thrown the deeper the call stack is?

So if an exception is thrown in the same function that catches it, would it basically be a goto?

Serve Laurijssen
  • 9,266
  • 5
  • 45
  • 98
  • 2
    This is interesting in terms of wanting to know minute details about how the jvm works, but from a practical standpoint, I'm not sure why it matters. Performance considerations of exceptions should never be a thing because they represent cases that should be very, very rare. If they aren't, then some requirements probably need to be rethought. – MadConan Jun 04 '15 at 13:48
  • 1
    Speaking from C# perspective, this is a yes/no answer, or "it depends". The "performance" is dependent on how far up the call stack the runtime needs to go to find the appropriate exception handler. Exceptions are always expensive and as the above comment says, performance should not be a concern because you should not use exceptions to dictate control flow. – Ron Beyer Jun 04 '15 at 14:05

1 Answers1

1

if an exception is thrown in the same function that catches it, would it basically be a goto?

No, a number of other things, including constructing a StackTraceElement[], are done when an exception is created. In theory you could throw a previously-existing exception which would avoid that sort of construction, but obviously that would defeat much of the purpose of throwing an exception in the first place.

dimo414
  • 47,227
  • 18
  • 148
  • 244