I started reading about CLR and was wondering how the runtime exceptions are thrown.
When there is any syntax error and if we try to build the program, the compiler detects it and throws the error. In this case the IL wont be generated by the compiler ( I assume that it wont create. Please clarify) But when there is a possibility of runtime error ( say division by zero or reference to the null) the compiler cannot detect this and produces IL.
While running the program JIT uses this IL and produces machine code. Now, when the machine code executes the step that has the division by zero, throws an exception.
When there is such an exception, it will be shown in the visual studio showing which line this exception has occured. How is this done??
Hope my question is clear.
Visual Studio creates PDB files, which contains mapping between machine code location and source code location for that instruction.
Just like we write code to check some condition and throw exception, for runtime exception, conditions are generated by jit and in machine language they check for the error and throw exception. So before every division operation, zero check will be made by runtime and exception will be thrown. To take advantage of latest CPUs with advanced technologies, these are implemented and executed differently instead of making it as explicit instruction in IL.
When there is such exception, clr maps the instruction in PDB file and returns source code. If you delete PDB and run from command line, you won’t see any line number information. Debugger is a program which loads clr code along with PDB and does all mapping. Visual Studio just gets line number and opens the file for you. There is extensive Debugger API, which you can use to get runtime values which visual studio displays.