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 />

trans.Rotate(env.LowerLeft, 1D);<br />

}<br />

catch (COMException COMex)<br />

{<br />

if (COMex.ErrorCode == -2147220984)<br />

MessageBox.Show("You cannot rotate an Envelope");<br />

MessageBox.Show ("Error " + COMex.ErrorCode.ToString() + ": " +<br />

COMex.Message);<br />

}<br />

catch (System.Exception ex)<br />

{<br />

MessageBox.Show("Error: " + ex.Message);<br />

}<br />

...<br />

The COMException class belongs to the System.Runtime.InteropServices<br />

namespace. It provides access to the value of the original HRESULT via the<br />

ErrorCode property, which you can test to find out which error condition occurred.<br />

Throwing errors and the exception hierarchy<br />

If you are coding a user interface, you may want to attempt to correct the error<br />

condition in code and try the call again. Alternatively, you may want to report the<br />

error to the user to let them decide which course of action to take; you can make<br />

use of the Message property of the Exception class to identify the problem.<br />

However, if you are writing a function that is only called from other code, you<br />

may want to deal with an error by creating a specific error condition and propagating<br />

this error to the caller. You can do this using the Throw keyword.<br />

To throw the existing error to the caller function, write your error handler using<br />

the Throw keyword, as shown below.<br />

[VB.NET]<br />

Catch ex As System.Exception<br />

...<br />

[C#]<br />

catch (System.Exception ex)<br />

{<br />

throw;<br />

}<br />

...<br />

If you want to propagate a different or more specific error back to the caller, you<br />

should create a new instance of an Exception class, populate it appropriately, and<br />

throw this exception back to the caller. The example shown below uses the<br />

ApplicationException constructor to set the Message property.<br />

[VB.NET]<br />

Catch ex As System.Exception<br />

Throw New ApplicationException _<br />

156 • <strong>ArcGIS</strong> <strong>Engine</strong> <strong>Developer</strong> <strong>Guide</strong>

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

Saved successfully!

Ooh no, something went wrong!