Emit Learning-advanced-Exception handling

Source: Internet
Author: User
Tags emit exception handling try catch

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.