Forum Discussion

jcook_105986's avatar
jcook_105986
Icon for Nimbostratus rankNimbostratus
May 16, 2008

how do i use the Exception method?

im new with C.

 

MY system environment is as follows...

 

 

OS: XP

 

system: intel core2duo

 

compiler: visual studio 2005

 

 

please review the code below.

 

 

///////////////////////////////////////////////////////////////////////

 

 

public iControl.Interfaces m_interfaces =

 

new iControl.Interfaces();

 

try

 

{

 

m_interfaces.initialize(tb_hostname.Text,

 

tb_username.Text, tb_password.Text);

 

 

}

 

catch (Exception ex)

 

{

 

iControlProgram.ExceptionInfo exi = new iControlProgram.ExceptionInfo(ex);

 

 

if (exi.IsiControlException)

 

{

 

string msg = "";

 

msg = msg + "\nException: " + exi.Exception;

 

msg = msg + "\nLocation : " + exi.Location;

 

msg = msg + "\nPrimary Error : " + exi.PrimaryErrorCode + "(" +

 

exi.PrimaryErrorCodeHex + ")";

 

msg = msg + "\nSeconary Error : " + exi.SecondaryErrorCode;

 

msg = msg + "\nDescription : " + exi.ErrorString;

 

MessageBox.Show(msg);

 

}

 

}

 

///////////////////////////////////////////////////////////////////////

 

 

Its not a big deal, really.

 

All im trying to do is, just login and if i fail,

 

then catch an exception and analyze the reason why.

 

 

For this, I copied the whole codes directly from the following link

 

( http://devcentral.f5.com/Default.aspx?tabid=63&articleType=ArticleView&articleId=119 )

 

 

Currently my BIG-IP F5 server is now broken.

 

Until the server is fixed, mean while I want to practice on

 

working with exceptions methods.

 

 

In above code, when login procedure fails the code does not

 

go into the catch region, but rather skip the region,

 

and exit the function.

 

 

Qurestion 1)

 

How come the iControl's initialize function does not throw any

 

exception when it fails? Do I need to throw my own exception handle,

 

or is there any other way to manually throw an iControl exception?

 

 

Question 2)

 

In the above code example, when I replace the initialize function

 

with the following code, the program actually falls into the catch region.

 

 

m_interfaces.NetworkingVLAN.get_vlan_id(new string[] { "foobar" });

 

 

However, the (Exception ex)handle is empty so it does not return

 

any error message. Does the iControl handle must be initialized

 

before being able to throw any exception? or...am I doing something

 

wrong here?

 

1 Reply

  • I think you'll be happy with C as it is an awesome language. Regarding your questions...

     

     

    Qurestion 1)

     

    How come the iControl's initialize function does not throw any

     

    exception when it fails? Do I need to throw my own exception handle,

     

    or is there any other way to manually throw an iControl exception?

     

     

    The initialize method in the iControl assembly does in fact trap exceptions and returns a true/false from the method to indicate whether the connection was successfully initialized. This includes making a call to the System.SystemInfo.get_version() method. Originally this method did have throw the connection exceptions but from user requests, I changed this to return a success value. I can't remember exactly the reason they wanted it this way but I seem to remember it having to do with using the assembly from a scripting environment that didn't handle exceptions well and just wanted to know a true/false if it succeeded. I guess I could throw a configuration flag in there to return the exception if one is caught but it's not that way right now.

     

     

    By the way, the source for the iControl assembly is available if you want to play around with it for yourself...

     

     

    Question 2)

     

    In the above code example, when I replace the initialize function

     

    with the following code, the program actually falls into the catch region.

     

     

    m_interfaces.NetworkingVLAN.get_vlan_id(new string[] { "foobar" });

     

     

     

    Now, that is different that the initialize method in that it is passing right down to the lower level proxy calls. The NetorkingVLAN member to m_interfaces is actually a property accessor returning the instance of the internal iControl.NetworkingVLAN proxy class. Since you are calling this directly, there is no catch around it inside the assembly so you'll get the exceptions passed back to your calling routine.

     

     

    I'm very surprised that you say that the catch triggered but the contents of the ex Exception object is empty. Are you sure about that? I've never seen a .Net app behave that way and didn't think it was possible. I'd stop it in the debugger and look at the contents of the ex variable and double check. you can also do a print out of ex.Message.ToString() to get the raw exception message.

     

     

    Hopefully this helps and if not, then please feel free to post again and let us know.

     

     

    -Joe