Exception handling is also a more important part of the program, today we will write the exception handling code with IL to explain, first of all, as usual, to implement the C # code of the class, as follows:
Exceptionhandler
Class Exceptionhandler
{public
static int ConvertToInt32 (string str)
{
int num = 0;
Try
{
num = Convert.ToInt32 (str);
The
catch (Exception ex)
{
Console.WriteLine (ex). message);
return
num;
}
}
The code is simpler, mainly to explain how to add a try, catch block in the IL code. This will use several new methods in the ILGenerator class, as described below:
L Beginexceptionblock: Used to indicate the beginning of code that requires exception handling, which is equivalent to the try keyword in C # code;
L EndExceptionBlock: This is paired with the Beginexceptionblock to indicate the end of the exception handling. Note: The end here is not just the end of a try statement block, but the end of the entire try Catch statement block;
L BeginCatchBlock: This is used to indicate the start of a catch operation, which needs to pass in a type that needs to be captured, and there can be multiple BeginCatchBlock in the program, that is, multiple catch blocks.