Exceptions: Difference between revisions

Content deleted Content added
Added Lua example
Line 946:
 
=={{header|PARI/GP}}==
===Catching errors in GP===
The errors that can be trapped in GP are:
{| class="wikitable"
|<code>alarmer</code>
|generic error
|-
|<code>gdiver</code>
|division by 0
|-
|<code>invmoder</code>
|impossible modular inverse
|-
|<code>archer</code>
|not available on this architecture or operating system
|-
|<code>typeer</code>
|wrong type
|-
|<code>errpile</code>
|the PARI stack overflows
|-
|<code>talker</code>
|generic error
|-
|<code>user</code>
|User-initiated error
|}
<lang parigp>trap(/* specific error can be given here, or leave blank to catch all */,
"caught"
Line 951 ⟶ 978:
error("bad stuff")
)</lang>
 
===Throwing errors in GP===
The only error that can be thrown in GP is user error:
<lang parigp>error("Text of error here")</lang>
 
===Throwing errors in PARI===
Many more errors can be caught and thrown directly in PARI:
<div style="height:20ex; width: 40em; max-width: 100%; overflow: scroll; overflow-x: hidden">
{| class="wikitable"
|<code>0</code>
|Generic error
|-
|<code>talker2</code>
|?
|-
|<code>bugparier</code>
|Bug, please report
|-
|<code>alarmer</code>
|Generic error
|-
|<code>openfiler</code>
|File I/O
|-
|<code>talker</code>
|Generic error
|-
|<code>flagerr</code>
|Invalid flag
|-
|<code>impl</code>
|Not implemented
|-
|<code>archer</code>
|Not available on this system
|-
|<code>notfuncer</code>
|Not a function in function call
|-
|<code>precer</code>
|Precision too low
|-
|<code>typeer</code>
|Incorrect type
|-
|<code>consister</code>
|Inconsistent data
|-
|<code>user</code>
|User-initiated error
|-
|<code>errpile</code>
|Stack overflow
|-
|<code>overflower</code>
|Overflow
|-
|<code>matinv1</code>
|Non-invertible matrix (in gauss)
|-
|<code>mattype1</code>
|Not a square matrix
|-
|<code>arither1</code>
|Not an integer argument in an arithmetic function
|-
|<code>primer1</code>
|Not enough precomputed primes
|-
|<code>invmoder</code>
|Impossible inverse
|-
|<code>constpoler</code>
|Constant polynomial
|-
|<code>notpoler</code>
|Not a polynomial
|-
|<code>redpoler</code>
|Reducible polynomial
|-
|<code>zeropoler</code>
|Zero polynomial
|-
|<code>operi</code>
|"Impossible"
|-
|<code>operf</code>
|"Forbidden"
|-
|<code>gdiver</code>
|Division by zero
|-
|<code>memer</code>
|Not enough memory
|-
|<code>negexper</code>
|Negative exponent
|-
|<code>sqrter5</code>
|Non quadratic residue (in gsqrt)
|-
|<code>noer</code>
|Not an error...
|}
</div>
<lang C>pari_err(arither1, "functionName"); // Gives "*** functionName: not an integer argument in an arithmetic function"</lang>
 
===Catching errors in PARI===
It is rare that this mechanism needs to be used in PARI, rather than standard [[#C|C]] methods, but the function <code>closure_trapgen</code> (similar to <code>closure_evalgen</code>) is available:
<lang C>GEN x = closure_trapgen(arither1, f); // Executes the function f, catching "not an integer argument in an arithmetic function" errors
if (x == (GEN)1L) // Was there an error?
pari_printf("Don't do that!\n"); // Recover</lang>
 
=={{header|Perl}}==