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 开发者_JAVA技巧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
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.
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.
精彩评论