Emit learning-advanced-Exception Handling

Source: Internet
Author: User
Tags emit try catch

Exception Handling is alsoProgramIs an important partIlWriting Exception HandlingCodeFirst, the class to be implemented is provided as an example.C #The Code is as follows:

Exceptionhandler
Class Exceptionhandler
{
Public   Static   Int Converttoint32 ( String Str)
{
Int Num =   0 ;
Try
{
Num = Convert. toint32 (STR );
}
Catch (Exception ex)
{
Console. writeline (ex. Message );
}
Return Num;
}
}

the code is relatively simple, it mainly describes how to IL MSO-Hansi-font-family: "Times New Roman" "> Add try "Times New Roman" ">, catch block. ilgenerator several new methods in the class, introduction:

L
beginexceptionblock "Times New Roman" ">: indicates the beginning of the Code for exception handling, which is equivalent to C # "Times New Roman" "> try "Times New Roman" "> keywords;


Endexceptionblock : Beginexceptionblock Pairing indicates the end of exception handling. Note: The end here is not just Try The end of the statement block refers to the entire Try catch The end of the statement block;


Begincatchblock : Indicates Catch At the beginning of the operation, this method needs to be passed in the type of a field to be captured, the program can have multiple Begincatchblock , That is, multiple Catch Block.

The related code is as follows:

Exception
Typebuilder = Modulebuilder. definetype ( " Exceptionhandler " , Typeattributes. Public );

methodbuilder = typebuilder. definemethod ( " convertint32 " , methodattributes. public | methodattributes. static, typeof (int32 ), New type [] { typeof ( string )});

Ilgenerator methodil=Methodbuilder. getilgenerator ();
Localbuilder num=Methodil. declarelocal (Typeof(Int32 ));

//Int num = 0;
Methodil. emit (Opcodes. ldc_i4_0 );
Methodil. emit (Opcodes. stloc_0 );

// Begin try
Label trylabel = Methodil. beginexceptionblock ();
// Num = convert. toint32 (STR );
Methodil. emit (Opcodes. ldarg_0 );
Methodil. emit (Opcodes. Call, Typeof (Convert). getmethod ( " Toint32 " , New Type [] { Typeof ( String )}));
Methodil. emit (Opcodes. stloc_0 );
// End try

// Begin catch Note: At this time, the top of the stack is exception information ex
Methodil. begincatchblock ( Typeof (Exception ));
// Console. writeline (ex. Message );
Methodil. emit (Opcodes. Call, Typeof (Exception). getmethod ( " Get_message " ));
Methodil. emit (Opcodes. Call, Typeof (Console). getmethod ( " Writeline " , New Type [] { Typeof ( String )}));
// End catch
Methodil. endexceptionblock ();

//Return num;
Methodil. emit (Opcodes. ldloc_0 );
Methodil. emit (Opcodes. Ret );

Type type=Typebuilder. createtype ();

Assemblybuilder. Save (filename );

download Source Code MSO-Fareast-font-family:; MSO-font-kerning: 6.5pt; MSO-ANSI-language: En-US;
MSO-Fareast-language: ZH-CN; MSO-bidi-language: AR-SA "> "Times New Roman" "> Exception Handling

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.