views:

81

answers:

6

Hi all Suppose an error is occurred in an line. if it is then the control goes to catch statement. so the question is how the error-ed line(from try block) knows to go to catch statement.. this is not a c#/java/c++ question. How it works internally,how the line knows to go catch statement.

thanks saj

+2  A: 

When an exception occurs a special instruction is executed (usually called interrupt). This leads to executing a generic error handler that deduces which is the latest installed suitable exception handler. That handler is then executed.

sharptooth
@sharptooth that is all are know.. the child handler execute first.. i'm expecting answer from system software/assembly/.net framework
saj
@sharptooth: could you explain that the special instruction that whatever you think
saj
@saj: In native C++ programs a special processor instruction `int` is used which is executed and halts the program and then control is automatically transferred to operating system interrupt handler which in turn sees that it's a C++ exception and transfers control to the C++ exception handler.
sharptooth
+5  A: 

this is not a c#/java/c++ question. How it works internally,how the line knows to go catch statement.

How this works internally makes this pretty much a c#/java/C++ question (because it will be implemented differently).

In Java, a try block installs itself into a special table (in the class file). When the JVM throws an exception, it looks at that table to see where the next catch or finally block to go to is.

Thilo
Good answer for Java!
fredley
A: 

it should work in all langues somewhat like this:

if (error_occured xy while doing things in try){
  call_catch_part(error xy)
}
Thariama
A: 

It is basically parsing fundamentals of the language. You can get all info at Here

Ashay
A: 

you could do the same in C even though there is no exception handling per se.

There you would use setjmp/longjmp unfortunately you then do not get the stack unwinding and have to handle all the nitty-gritty yourself.

Anders K.
A: 

There is a difference how exceptions are technically handled between natively compiled languages such as C++ and languages using byte-code being executed on a virtual machine such as Java or C#.

C++ compilers usually generate code that protocols the information needed for exception handling at runtime. A dedicated data structure is used to remember entrance/exit of try blocks and the associated exception handler. When an exception occurs, an interrupt is generated and control is passed to the OS which in turn inspects the call stack and determines which exception handler to call.

Further details are pretty well explained in the following article by Vishal Kochhar:

How a C++ compiler implements exception handling

In Java or .NET there is no need for the overhead of maintaining exception handling information as the runtime will be able to introspect the byte code to find the relevant exception handler. As a consequence, only exceptions that are actually thrown are causing an overhead.

0xA3

related questions