Invincible
Invincible

Reputation: 815

Run time exception handling (like Divide by zero) in JVM (Java)or CLR (C#)

How does JVM or for that matter CLR (Common language runtime) handles divided by zero? Does it check denominator every time, before executing divide instruction? Or is it handled using call back function which get invoked when "divide by zero" trap raised by processor?

Any input will be appreciated.

Thank you, Alan

Upvotes: 4

Views: 591

Answers (2)

Hans Passant
Hans Passant

Reputation: 941317

All processors I know generate a hardware trap for this. Which is handled by the operating system and reflected into the user mode code with an OS dependent mechanism (signal, exception, etc). Which the runtime picks up and translates into a Java or .NET exception.

Upvotes: 7

lornova
lornova

Reputation: 6933

It is an implementation detail, as you should not care of what happens under the hood of the virtual machine (if you had to care about it, you would lose true portability!).

However, since managed code is jitted, the division is always performed and in case of zero denominator the processor will throw a first chance exception that will be caught by the runtime and then reflected as a high level exception.

Upvotes: 2

Related Questions