18.04.2015 Views

ArcGIS Engine Developer Guide

ArcGIS Engine Developer Guide

ArcGIS Engine Developer Guide

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

.NET APPLICATION PROGRAMMING INTERFACE<br />

[C#]<br />

{<br />

IEnvelope env = new EnvelopeClass();<br />

env.PutCoords(0D, 0D, 10D, 10D);<br />

ITransform2D trans = (ITransform2D) env;<br />

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

}<br />

catch (System.Exception ex)<br />

{<br />

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

}<br />

{<br />

}<br />

// Perform any tidy up of code.<br />

You place a try block around code that may fail. If the application throws an<br />

error within the Try block, the point of execution will switch to the first Catch<br />

block.<br />

The Catch block handles a thrown error. The application executes the Catch<br />

block when the Type of a thrown error matches the Type of error specified by<br />

the Catch block. You can have more than one Catch block to handle different<br />

kinds of errors. The code shown below checks first if the exception thrown is a<br />

DivideByZeroException.<br />

[VB.NET]<br />

...<br />

Catch divEx As DivideByZeroException<br />

' Perform divide by zero error handling.<br />

Catch ex As System.Exception<br />

' Perform general error handling.<br />

...<br />

[C#]<br />

...<br />

catch (DivideByZeroException divEx)<br />

{<br />

// Perform divide by zero error handling.<br />

}<br />

catch (System.Exception ex)<br />

{<br />

// Perform general error handling.<br />

}<br />

...<br />

If you do have more than one Catch block, note that the more specific exception,<br />

Types, should precede the general System.Exception, which will always succeed<br />

the type check.<br />

The application always executes the Finally block, either after the Try block<br />

completes or after a Catch block, if an error was thrown. The Finally block<br />

should, therefore, contain code that must always be executed, for example, to<br />

154 • <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!