Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Exception type from an exception

Tags:

c#

I have a application to connect SAP with an RFC call and I need to show a notification to the user when connection failed while try to establish the RFC call with SAP. And I'm getting the following exception.

{
    SAP.Middleware.Connector.RfcCommunicationException: 
    LOCATION    CPIC (TCP/IP) on local host with Unicode
    ERROR       partner '151.9.39.8:8010' not reached
    TIME        Wed Jul 16 10:32:05 2014
    RELEASE     720
    COMPONENT   NI (network interface)
    VERSION     40
    RC          -10
    MODULE      nixxi.cpp
    LINE        3286
    DETAIL      NiPConnect2: 151.9.39.8:8010
    SYSTEM CALL connect
    ERRNO       10060
    ERRNO TEXT  WSAETIMEDOUT: Connection timed out
    COUNTER     2
} 

And by using this exception I need to notify the user. But how can I identify whether it is a SAP.Middleware.Connector.RfcCommunicationException or not because I'm handling other exceptions too. Is there any way to get the type of the exception without concatenating the above exception string.

In my try catch block I'm currently doing this but it is not working.

catch (Exception ex)
{  
    if (ex.ToString().ToLower() == "rfccommunicationexception")
    {
        MessageError = "RFC error";
    }
}
like image 329
tarzanbappa Avatar asked Mar 25 '26 19:03

tarzanbappa


2 Answers

Catch the exception explicitly:

catch(SAP.Middleware.Connector.RfcCommunicationException)
{
    // RFC exception
}
catch(Exception e)
{
    // All other exceptions
} 
like image 70
zmbq Avatar answered Mar 27 '26 09:03

zmbq


The best approach to this is to have multiple catch blocks:

try
{
   // your code
}
catch(RfcCommunicationException rfcEx)
{
  // handle rfc communication exception
}
cathc(Exception ex)
{
  // handle other exception
}
like image 36
malkassem Avatar answered Mar 27 '26 07:03

malkassem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!