| 25 | Make your code robust to failures by encapsulating big function calls in [http://www.homeandlearn.co.uk/net/nets5p4.html Try / Catch] blocks. Public methods on public classes are often the entry point to deeper functionality, and placing a Try / Catch inside the public methods is a good way to capture any internal failures within your code. We've done the same in the plug-in framework, where every plug-in point call is wrapped in a Try / Catch block to make sure EwE is not affected by a failing plug-in point or button click. |
| 26 | |
| 27 | Take for instance the code that runs in response to a 'Run' button click on the Run Ecosim form: |
| 28 | {{{ |
| 29 | Private Sub OnRun(ByVal sender As System.Object, ByVal e As System.EventArgs) _ |
| 30 | Handles m_btnRun.Click |
| 31 | |
| 32 | Try |
| 33 | If Not Me.IsRunning Then |
| 34 | Me.m_iTimeSteps = Me.Core.nEcosimTimeSteps |
| 35 | Me.m_graph.Refresh() |
| 36 | Me.Core.RunEcoSim(AddressOf TimeStepFromEcoSim_handler, True) |
| 37 | End If |
| 38 | Catch ex As Exception |
| 39 | cLog.Write(ex, "form RunEcosim.OnRun") |
| 40 | End Try |
| 41 | |
| 42 | End Sub |
| 43 | }}} |
| 44 | |
| 45 | Note that in EwE we prefer not to rely on exceptions for regular error testing. Exception handling is very processor intensive, and a simple [http://www.homeandlearn.co.uk/net/nets1p20.html If / Then] test is endlessly faster than bluntly executing code and waiting for the exceptions to happen. As such, we suggest to only use exceptions to capture unforeseen problems. |
| 46 | |
| 47 | === Logging events === |
| 48 | |
| 49 | The EwE Log file, created and managed by the core, and stored in the Windows application data folder, contains a track list of important actions taken by !EwE in response to user requests and a track record of failures and successes. The purpose of the log is to provide postmortem diagnostics when an error has occurred. You can use the log as well in your plug-ins. Frankly, we think you should, because |