Try...Catch: Difference between revisions
Jump to navigation
Jump to search
Created page with "TRY <br /> :::{''statements''} <br /> :::THROW ''errCode'' <br /> CATCH ''err'' <br /> :::{''statements''} <br /> [FINALLY <br /> :::{''statements''}] <br /> END TRY '''Descr..." |
No edit summary |
||
Line 1: | Line 1: | ||
Try <br /> | |||
:::{''statements''} <br /> | :::{''statements''} <br /> | ||
::: | :::Throw ''errCode'' <br /> | ||
Catch ''err'' <br /> | |||
:::{''statements''} <br /> | :::{''statements''} <br /> | ||
[ | [Finally <br /> | ||
:::{''statements''}] <br /> | :::{''statements''}] <br /> | ||
End Try | |||
== Description == | |||
The | The Try…Catch statement allows you to test a block of code for runtime errors. The Try block contains the code to be run, and the CATCH block contains the code to be executed if an error occurs. The optional Finally block is executed whether there is an error or not. | ||
An error can be created by something that gets executed (such as division by zero) or you can force an error using the | An error can be created by something that gets executed (such as division by zero) or you can force an error using the Throw statement. When you force an error using THROW, supply an ''errCode'' which can be a number or a string. | ||
The CATCH block has the value of the runtime error or the THROW message you supplied in ''err''. | The CATCH block has the value of the runtime error or the THROW message you supplied in ''err''. | ||
== Example == | |||
<pre> | <pre> | ||
Rem Try Example | |||
' | 'Try catches run time errors | ||
Try | Try | ||
Dim CurrLiab, CurrAssets | Dim CurrLiab, CurrAssets | ||
Line 45: | Line 45: | ||
</pre> | </pre> | ||
== Output == | |||
<pre> | <pre> | ||
Line 51: | Line 51: | ||
Done | Done | ||
</pre> | </pre> | ||
[[Category:Language Reference]] |
Revision as of 04:01, 17 August 2012
Try
- {statements}
- Throw errCode
- {statements}
Catch err
- {statements}
- {statements}
[Finally
- {statements}]
- {statements}]
End Try
Description
The Try…Catch statement allows you to test a block of code for runtime errors. The Try block contains the code to be run, and the CATCH block contains the code to be executed if an error occurs. The optional Finally block is executed whether there is an error or not.
An error can be created by something that gets executed (such as division by zero) or you can force an error using the Throw statement. When you force an error using THROW, supply an errCode which can be a number or a string.
The CATCH block has the value of the runtime error or the THROW message you supplied in err.
Example
Rem Try Example 'Try catches run time errors Try Dim CurrLiab, CurrAssets CurAssets=InputBox("Enter assets") If Not IsNumeric(CurAssets) Then Throw 1 If CurAssets < 0 Then Throw "lessthan" CurLiab=InputBox("Enter liabilities") If CurLiab=0 Then Throw "Divide by Zero" If CurLiab < 0 Then Throw "Liabilities must be > 0" End If If Not IsNumeric(CurLiab) Then Throw 1 Print CurAssets, CurLiab Print "Ratio:" & CurAssets/CurLiab Catch err If err=1 Then err="Enter a number" If err="lessthan" Then err="Value must be >= 0" End if Print err Finally 'this is optional Print "Done" End Try
Output
(depends on what values are input) Done