18.04.2015 Views

ArcGIS Engine Developer Guide

ArcGIS Engine Developer Guide

ArcGIS Engine Developer Guide

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

.NET APPLICATION PROGRAMMING INTERFACE<br />

("You had an error in your application")<br />

...<br />

[C#]<br />

catch (System.Exception ex)<br />

{<br />

throw new ApplicationException("You had an error in your application");<br />

}<br />

...<br />

If you do this, however, the original exception is lost. To allow complete error<br />

information to be propagated, the Exception class includes the InnerException<br />

property. This property should be set to equal the caught exception, before the<br />

new exception is thrown. This creates an error hierarchy. Again, the example<br />

shown below uses the ApplicationException constructor to set the<br />

InnerException and Message properties.<br />

[VB.NET]<br />

Catch ex As System.Exception<br />

Dim appEx As System.ApplicationException = _<br />

New ApplicationException("You had an error in your application", ex)<br />

Throw appEx<br />

...<br />

[C#]<br />

catch (System.Exception ex)<br />

{<br />

System.ApplicationException appEx =<br />

new ApplicationException("You had an error in your application", ex);<br />

throw appEx;<br />

}<br />

...<br />

In this way, the function that eventually deals with the error condition can access<br />

all the information about the cause of the condition and its context.<br />

If you throw an error, the application will execute the current function’s Finally<br />

clause before control is returned to the calling function.<br />

System.__ComObject and casting to strongly typed RCWs<br />

Sometimes you may find that casting a variable fails when you think it should<br />

succeed (the solution is often to declare variables as interface Types and avoid the<br />

use of class types, for example, use IStyleGallery rather than StyleGalleryClass).<br />

You may also have come across the System.__ComObject type and wonder where<br />

it comes from. This topic should help you to understand these issues; in particular,<br />

you may encounter problems when attempting to create the AppRef class in<br />

.NET—this issue is related to the System.__ComObject wrapper and is also<br />

covered below.<br />

Types and Runtime Callable Wrappers<br />

In .NET, each class, interface, enumeration, and so on, is described by its Type.<br />

The Type class, which is part of the .NET Framework, holds information about<br />

Chapter 4 • <strong>Developer</strong> environments • 157

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!