Try...Catch: Difference between revisions

From NSB App Studio
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
 
(6 intermediate revisions by 4 users not shown)
Line 1: Line 1:
TRY <br />
Try <br />
:::{''statements''} <br />
:::{''statements''} <br />
:::THROW ''errCode'' <br />
:::Throw ''errCode'' <br />
CATCH ''err'' <br />
Catch ''err'' <br />
:::{''statements''} <br />
:::{''statements''} <br />
[FINALLY <br />
[Finally <br />
:::{''statements''}] <br />
:::{''statements''}] <br />
END TRY
End Try
 
== Description ==


'''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.


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.


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


'''Example'''
<tabber>
JavaScript=
<syntaxhighlight lang="JavaScript">
// Try Example
/* Try catches run time errors */


<pre>
try {
REM TRY Example
  var CurrLiab, CurrAssets;
'TRY catches run time errors
  CurAssets=prompt("Enter assets","");
  if(!(IsNumeric(CurAssets)) ){ throw 1; }
  if(CurAssets < 0) { throw "lessthan"; }
  CurLiab=prompt("Enter liabilities","");
  if(CurLiab==0) { throw "Divide by Zero"; }
  if(CurLiab < 0) {
      throw "Liabilities must be > 0";
  }
  if(!(IsNumeric(CurLiab) ) ){ throw 1; }
  NSB.Print(CurAssets+ " " + CurLiab);
  NSB.Print("Ratio:" + (CurAssets/CurLiab));
} catch(err) {
  if(err==1) { err="Enter a number"; }
  if(err=="lessthan") {
      err="Value must be >= 0";
  }
  NSB.Print(err);
} finally { //this is optional
  NSB.Print("Done");
}</syntaxhighlight>
|-|
BASIC=
<syntaxhighlight lang="vb.net">
Rem Try Example
'Try catches run time errors
Try
Try
   Dim CurrLiab, CurrAssets  
   Dim CurrLiab, CurrAssets  
Line 42: Line 72:
Finally 'this is optional
Finally 'this is optional
   Print "Done"
   Print "Done"
End Try
End Try</syntaxhighlight>
</pre>
</tabber>


'''Output'''
== Output ==


<pre>
<pre>
Line 51: Line 81:
Done
Done
</pre>
</pre>
[[Category:Language Reference]]
[[Category:Statements - Flow of control]]

Latest revision as of 23:32, 24 July 2019

Try

{statements}
Throw errCode

Catch err

{statements}

[Finally

{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

// Try Example
/* Try catches run time errors */

try {
  var CurrLiab, CurrAssets;
   CurAssets=prompt("Enter assets","");
   if(!(IsNumeric(CurAssets)) ){ throw 1; }
   if(CurAssets < 0) { throw "lessthan"; }
   CurLiab=prompt("Enter liabilities","");
   if(CurLiab==0) { throw "Divide by Zero"; }
   if(CurLiab < 0) {
      throw "Liabilities must be > 0";
   }
   if(!(IsNumeric(CurLiab) ) ){ throw 1; }
   NSB.Print(CurAssets+ " " + CurLiab);
   NSB.Print("Ratio:" + (CurAssets/CurLiab));
} catch(err) {
   if(err==1) { err="Enter a number"; }
   if(err=="lessthan") {
      err="Value must be >= 0";
   }
   NSB.Print(err);
} finally { //this is optional
   NSB.Print("Done");
}

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