. Net connection to SAP system topic: Getting custom exceptions in RFC (4)

Source: Internet
Author: User

Sometimes we need to throw some custom exceptions in RFC. For example, if we enter a nonexistent product number, we need to throw an exception to inform the user that the product number does not exist. One stupid way is to judge through the return value, but this is not flexible. At the same time, the RFC will continue because the product number does not exist. If we can throw an exception on our own and the program automatically stops executing the RFC, it will be the best of both worlds!

Set a function: enter two numbers. The first number is divided by the second number. Two exceptions: the divisor cannot be 0 or the divisor cannot be 100.

First, create an RFC in se37 and use the RFC in the previous blog.

Set the import page as follows:

The export page is set as follows:

The changing and table pages are empty.

The exceptions page is set as follows:

Then there is the source code:

In the code, raise is used to throw an exception. The program stops running automatically and all returned values are cleared! The content after raise is defined in exceptions.

Then remember to activate the program !! So the sap end is over.

Return to vs2010 and create a program. The interface is as follows:

Then, start to write the program:

The first is to reference two DLL (refer to the previous article), and input at the beginning of the program:

Using SAP. Middleware. connector;

Public void NCO ()

{

Idestinationconfiguration id = new mybackendconfig ();

Rfcdestinationmanager. registerdestinationconfiguration (ID );

Rfcdestination PRD = rfcdestinationmanager. getdestination ("prd_000 ");

Rfcdestinationmanager. unregisterdestinationconfiguration (ID); // unregister

NCO (PRD );

}

Public void NCO (rfcdestination PRD)

{

String type = string. empty;

Rfcrepository repo = PRD. repository;

Irfcfunction companybapi = Repo. createfunction ("zrfc_mara_info"); // specify the RFC name

Try

{

Companybapi. setvalue ("num1", textbox1.text. Trim (); // copy input parameters

Companybapi. setvalue ("num2", textbox2.text. Trim (); // copy input parameters

Companybapi. Invoke (PRD); // start call execution

Textbox3.text = companybapi. getvalue ("num3"). tostring (); // get the returned result

}

Catch (rfcabapexception ex) // this exception is used to obtain user-defined exception information !!!!

{

// Companybapi. Metadata. getabapexception (ex. Key). Documentation

MessageBox. Show (companybapi. Metadata. getabapexception (ex. Key). Documentation, "sap RFC return information", messageboxbuttons. OK, messageboxicon. Error );

}

Catch (rfctypeconversionexception ex) // this exception is specially used to obtain the exception of variable type conversion !!!!

{

MessageBox. Show ("the value you entered is not a numerical value", "sap RFC return information", messageboxbuttons. OK, messageboxicon. Warning );

}

Catch (rfcabapruntimeexception ex) // this exception is used to obtain the runtime exception during RFC execution !!!!

{

MessageBox. Show (companybapi. Metadata. getabapexception (ex. Key). Documentation, "sap RFC return information", messageboxbuttons. OK, messageboxicon. Warning );

}

Catch (rfcbaseexception ex) // this exception is a total exception class. You can obtain all exceptions. If there are multiple catch exceptions, you cannot put the first catch !!!!

{

MessageBox. Show ("all other errors", "sap RFC return information", messageboxbuttons. OK, messageboxicon. Warning );

}

PRD = NULL;

Repo = NULL;

}

Public class mybackendconfig: idestinationconfiguration

{

Public rfcconfigparameters getparameters (string destinationname)

{

If ("prd_000". Equals (destinationname ))

{

Rfcconfigparameters parms = new rfcconfigparameters ();

Parms. Add (rfcconfigparameters. appserverhost, "192.168.1.3 ");

Parms. Add (rfcconfigparameters. systemnumber, "00 ");

Parms. Add (rfcconfigparameters. User, "mengxin ");

Parms. Add (rfcconfigparameters. password, "5239898 ");

Parms. Add (rfcconfigparameters. Client, "888 ");

Parms. Add (rfcconfigparameters. Language, "ZH ");

Parms. Add (rfcconfigparameters. poolsize, "5 ");

Parms. Add (rfcconfigparameters. maxpoolsize, "10 ");

Parms. Add (rfcconfigparameters. idletimeout, "60 ");

Return parms;

}

Else return NULL;

}

Public bool changeeventssupported ()

{

Return false;

}

Public event rfcdestinationmanager. configurationchangehandler configurationchanged;

}

Private void button#click (Object sender, eventargs E)

{

NCO ();

}

In this case, the C # running result is as follows:

The above information can be obtained correctly. Program test OK !!!

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.