Error trapping with Dyalog APL: Difference between revisions

From APL Wiki
Jump to navigation Jump to search
m (Text replacement - "</source>" to "</syntaxhighlight>")
Line 27: Line 27:
⎕FX 'r←WeakInterrupt' 'r←1002'
⎕FX 'r←WeakInterrupt' 'r←1002'
⎕FX 'r←StrongInterrupt' 'r←1003'
⎕FX 'r←StrongInterrupt' 'r←1003'
</source>
</syntaxhighlight>


In a large system you want those to be constants, so a user cannot change them. That's why they are niladic functions.
In a large system you want those to be constants, so a user cannot change them. That's why they are niladic functions.
Line 35: Line 35:
<source lang=apl>
<source lang=apl>
#.Events.RestartAppl←501
#.Events.RestartAppl←501
</source>
</syntaxhighlight>


According to the help file, users should use the range from 500 to 999 to define their own events.
According to the help file, users should use the range from 500 to 999 to define their own events.


== Setting <source lang=apl inline>⎕TRAP</source> ==
== Setting <source lang=apl inline>⎕TRAP</syntaxhighlight> ==


<source lang=apl inline>⎕TRAP</source> allows us to implement a general mechanism on a global level. For discussion purposes let's assume the following:
<source lang=apl inline>⎕TRAP</syntaxhighlight> allows us to implement a general mechanism on a global level. For discussion purposes let's assume the following:
# <source lang=apl inline>⎕LX</source> is set to run function <source lang=apl inline>Run</source>
# <source lang=apl inline>⎕LX</syntaxhighlight> is set to run function <source lang=apl inline>Run</syntaxhighlight>
# This function calls 3 sub-functions: <source lang=apl inline>Initial</source>, <source lang=apl inline>Work</source> and <source lang=apl inline>Shutdown</source>
# This function calls 3 sub-functions: <source lang=apl inline>Initial</syntaxhighlight>, <source lang=apl inline>Work</syntaxhighlight> and <source lang=apl inline>Shutdown</syntaxhighlight>
# <source lang=apl inline>Initial</source> initializes the application: it opens files, interprets an INI file, takes the Windows registry into account, builds the GUI and so forth.
# <source lang=apl inline>Initial</syntaxhighlight> initializes the application: it opens files, interprets an INI file, takes the Windows registry into account, builds the GUI and so forth.
# <source lang=apl inline>Work</source> simply runs <source lang=apl inline>⎕DQ</source>
# <source lang=apl inline>Work</syntaxhighlight> simply runs <source lang=apl inline>⎕DQ</syntaxhighlight>
# <source lang=apl inline>Shutdown</source> cleans up: it closes files, says good-by.
# <source lang=apl inline>Shutdown</syntaxhighlight> cleans up: it closes files, says good-by.


=== Solving the stop vector problem ===
=== Solving the stop vector problem ===
Line 53: Line 53:
<source lang=apl>
<source lang=apl>
⎕TRAP←⊂(#.Events.StopVector 'E' '→⎕LC')
⎕TRAP←⊂(#.Events.StopVector 'E' '→⎕LC')
</source>
</syntaxhighlight>
<source lang=apl inline>#.Events.StopVector</source> returns 1001 which is the event number a stop vector is associated with. As soon as APL stops on a stop vector <source lang=apl inline>⎕EN</source> is set to 1001. This event can be caught with <source lang=apl inline>⎕TRAP</source>, so we can tell APL to execute (<source lang=apl inline>'E'</source>) the expression given as third argument. In this case it tells APL to simply ignore stop vectors by resuming execution.
<source lang=apl inline>#.Events.StopVector</syntaxhighlight> returns 1001 which is the event number a stop vector is associated with. As soon as APL stops on a stop vector <source lang=apl inline>⎕EN</syntaxhighlight> is set to 1001. This event can be caught with <source lang=apl inline>⎕TRAP</syntaxhighlight>, so we can tell APL to execute (<source lang=apl inline>'E'</syntaxhighlight>) the expression given as third argument. In this case it tells APL to simply ignore stop vectors by resuming execution.


=== Preventing users from interrupting an application ===
=== Preventing users from interrupting an application ===
Line 64: Line 64:
events←#.Events.WeakInterrupt #.Events.StrongInterrupt
events←#.Events.WeakInterrupt #.Events.StrongInterrupt
⎕TRAP,←⊂(events 'E' '→⎕LC')
⎕TRAP,←⊂(events 'E' '→⎕LC')
</source>
</syntaxhighlight>


=== Restarting the application ===
=== Restarting the application ===


For reasons explained in a minute we now have to define the “Restart the application” procedure. For this, for the first time we do not use the <source lang=apl inline>'E'</source> statement but the <source lang=apl inline>'C'</source> statement. The ''C'' is short for ''Cut back''. This instructs APL to cut the status indicator back to the level where <source lang=apl inline>⎕TRAP</source> '''is localized''' – that is not  necessarily where it was set – and execute the expression in the 3rd argument there. However, if <source lang=apl inline>⎕TRAP</source> is not localized at all, i.e. it is in the workspace, the status indicator is cut back completely and the expression is executed in the workspace.
For reasons explained in a minute we now have to define the “Restart the application” procedure. For this, for the first time we do not use the <source lang=apl inline>'E'</syntaxhighlight> statement but the <source lang=apl inline>'C'</syntaxhighlight> statement. The ''C'' is short for ''Cut back''. This instructs APL to cut the status indicator back to the level where <source lang=apl inline>⎕TRAP</syntaxhighlight> '''is localized''' – that is not  necessarily where it was set – and execute the expression in the 3rd argument there. However, if <source lang=apl inline>⎕TRAP</syntaxhighlight> is not localized at all, i.e. it is in the workspace, the status indicator is cut back completely and the expression is executed in the workspace.
<source lang=apl>
<source lang=apl>
⎕TRAP,←⊂(#.Events.RestartAppl 'C' '→∆Restart')
⎕TRAP,←⊂(#.Events.RestartAppl 'C' '→∆Restart')
</source>
</syntaxhighlight>
To make this work the function in which <source lang=apl inline>⎕TRAP</source> is localized must have a label <source lang=apl inline>∆Restart</source> or a function that returns a valid line number to branch to of course.
To make this work the function in which <source lang=apl inline>⎕TRAP</syntaxhighlight> is localized must have a label <source lang=apl inline>∆Restart</syntaxhighlight> or a function that returns a valid line number to branch to of course.


=== Catching Errors ===
=== Catching Errors ===
Line 79: Line 79:
<source lang=apl>
<source lang=apl>
⎕TRAP,←⊂((0 1000) 'E' '#.HandleError' )
⎕TRAP,←⊂((0 1000) 'E' '#.HandleError' )
</source>
</syntaxhighlight>
The 0 stands for all the events from 1 to 999 while the 1000 stands for all events larger than 1000.
The 0 stands for all the events from 1 to 999 while the 1000 stands for all events larger than 1000.


<source lang=apl inline>⎕TRAP</source> may contain more than one error catching group. Since the contents of <source lang=apl inline>⎕TRAP</source> is scanned from left to right, a statement will ONLY be executed for an event not processed earlier. That is the reason why we must define the restart event first.
<source lang=apl inline>⎕TRAP</syntaxhighlight> may contain more than one error catching group. Since the contents of <source lang=apl inline>⎕TRAP</syntaxhighlight> is scanned from left to right, a statement will ONLY be executed for an event not processed earlier. That is the reason why we must define the restart event first.


For example, in the following statement:
For example, in the following statement:
<source lang=apl>
<source lang=apl>
⎕TRAP←(333 'E' 'expA') (0 'E' 'expB')
⎕TRAP←(333 'E' 'expA') (0 'E' 'expB')
</source>
</syntaxhighlight>
event 333 will be caught by the 1st group and NOT by the 2nd even though 0 stands for “events from 1 to 999”. Only the expression <source lang=apl inline>expA</source> will be executed.
event 333 will be caught by the 1st group and NOT by the 2nd even though 0 stands for “events from 1 to 999”. Only the expression <source lang=apl inline>expA</syntaxhighlight> will be executed.


== The <source lang=apl inline>#.HandleError</source> function ==
== The <source lang=apl inline>#.HandleError</syntaxhighlight> function ==


The <source lang=apl inline>#.HandleError</source> function should do at least the following:
The <source lang=apl inline>#.HandleError</syntaxhighlight> function should do at least the following:


* Perhaps neutralize <source lang=apl inline>⎕LX</source>
* Perhaps neutralize <source lang=apl inline>⎕LX</syntaxhighlight>
* Save the <source lang=apl inline>⎕DM</source> and <source lang=apl inline>⎕EN</source> settings
* Save the <source lang=apl inline>⎕DM</syntaxhighlight> and <source lang=apl inline>⎕EN</syntaxhighlight> settings
* Save the snapshot
* Save the snapshot
* Maybe create an HTML page with general information about the error
* Maybe create an HTML page with general information about the error
Line 110: Line 110:
Keep in mind that you want to have an easy opportunity to test the system with error trapping. So you may need another parameter that tells the system that error trapping has to be used. Last but not least, there should be an easy opportunity to let the application crash on purpose. I prefer to have a “developers menu”, which is displayed only to developers. Among other useful commands it offers a “Let's crash” option.
Keep in mind that you want to have an easy opportunity to test the system with error trapping. So you may need another parameter that tells the system that error trapping has to be used. Last but not least, there should be an easy opportunity to let the application crash on purpose. I prefer to have a “developers menu”, which is displayed only to developers. Among other useful commands it offers a “Let's crash” option.


=== Control Structure <source lang=apl inline>:Trap</source> ===
=== Control Structure <source lang=apl inline>:Trap</syntaxhighlight> ===


If you use <source lang=apl inline>:Trap</source>, keep in mind that <source lang=apl inline>⎕TRAP</source> and <source lang=apl inline>:Trap</source> are both taken into account. That means that in case of
If you use <source lang=apl inline>:Trap</syntaxhighlight>, keep in mind that <source lang=apl inline>⎕TRAP</syntaxhighlight> and <source lang=apl inline>:Trap</syntaxhighlight> are both taken into account. That means that in case of
<source lang=apl>
<source lang=apl>
:Trap 0
:Trap 0
Line 119: Line 119:
.
.
:EndTrap
:EndTrap
</source>
</syntaxhighlight>


the error caused by the <source lang=apl inline>-'a'</source> statement is caught by the <source lang=apl inline>:Else</source>, while the <source lang=apl inline>.</source> is caught by the <source lang=apl inline>⎕TRAP</source> setting.
the error caused by the <source lang=apl inline>-'a'</syntaxhighlight> statement is caught by the <source lang=apl inline>:Else</syntaxhighlight>, while the <source lang=apl inline>.</syntaxhighlight> is caught by the <source lang=apl inline>⎕TRAP</syntaxhighlight> setting.


When using <source lang=apl inline>:Trap</source> try to be as specific as possible. For example, this code is faulty:
When using <source lang=apl inline>:Trap</syntaxhighlight> try to be as specific as possible. For example, this code is faulty:
<source lang=apl>
<source lang=apl>
:Trap 0
:Trap 0
Line 130: Line 130:
filename ⎕FCREATE 0
filename ⎕FCREATE 0
:EndTrap
:EndTrap
</source>
</syntaxhighlight>
because it tries to create a file not only if this file does not already exist but also if the current user lacks the right to tie it, for example because somebody else has already tied it exclusively. Therefore, it is a better to be specific:
because it tries to create a file not only if this file does not already exist but also if the current user lacks the right to tie it, for example because somebody else has already tied it exclusively. Therefore, it is a better to be specific:
<source lang=apl>
<source lang=apl>
Line 138: Line 138:
filename ⎕FCREATE 0
filename ⎕FCREATE 0
:EndTrap
:EndTrap
</source>
</syntaxhighlight>
The best idea, however, is to use <source lang=apl inline>⎕NEXISTS</source> to check the file for already being created. In general it is a good idea to use error trapping only for extraordinary problems.
The best idea, however, is to use <source lang=apl inline>⎕NEXISTS</syntaxhighlight> to check the file for already being created. In general it is a good idea to use error trapping only for extraordinary problems.


=== The <source lang=apl inline>⎕SIGNAL</source> system function ===
=== The <source lang=apl inline>⎕SIGNAL</syntaxhighlight> system function ===


Note that an event which is <source lang=apl inline>⎕SIGNAL</source>led can be intercepted with <source lang=apl inline>⎕TRAP</source> but not <source lang=apl inline>:Trap</source>
Note that an event which is <source lang=apl inline>⎕SIGNAL</syntaxhighlight>led can be intercepted with <source lang=apl inline>⎕TRAP</syntaxhighlight> but not <source lang=apl inline>:Trap</syntaxhighlight>
If you execute this function:
If you execute this function:
<source lang=apl>
<source lang=apl>
Line 154: Line 154:
  :EndTrap
  :EndTrap
</source>
</syntaxhighlight>
you get this:
you get this:
<source lang=apl>
<source lang=apl>
caught by ⎕TRAP
caught by ⎕TRAP
</source>
</syntaxhighlight>


=== Ensure future trouble ===
=== Ensure future trouble ===
Line 167: Line 167:
DoSomethingHere
DoSomethingHere
:EndTrap
:EndTrap
</source>
</syntaxhighlight>
This technique is called “silent trapping”. If something is going wrong, do not take care and do not tell anybody about it!
This technique is called “silent trapping”. If something is going wrong, do not take care and do not tell anybody about it!


Line 180: Line 180:
TakeCare
TakeCare
EndTrap
EndTrap
</source>
</syntaxhighlight>
If the flag is true, error trapping is active, if not, the <source lang=apl inline>DoSomeThing</source> statement will fail if an error occurs. This makes is much easier to debug an application.
If the flag is true, error trapping is active, if not, the <source lang=apl inline>DoSomeThing</syntaxhighlight> statement will fail if an error occurs. This makes is much easier to debug an application.


You might need a more sophisticated mechanism for this, because under some circumstances you want to switch off most but not all error trapping statements. For example, if you use a logging mechanism which is logging every user action for analyzing purposes, the code doing this may cause an interrupt itself, for example because the disk is full which holds the logging files. In such a case it might be inappropriate that the logging code breaks the application. Therefore, you might control this code with <source lang=apl inline>:Trap</source>-statements.
You might need a more sophisticated mechanism for this, because under some circumstances you want to switch off most but not all error trapping statements. For example, if you use a logging mechanism which is logging every user action for analyzing purposes, the code doing this may cause an interrupt itself, for example because the disk is full which holds the logging files. In such a case it might be inappropriate that the logging code breaks the application. Therefore, you might control this code with <source lang=apl inline>:Trap</syntaxhighlight>-statements.


In such a case it might be a good idea to control the behavior of the application on different levels for code which is really essential in terms of business logic, for example, and for code which is not essential.
In such a case it might be a good idea to control the behavior of the application on different levels for code which is really essential in terms of business logic, for example, and for code which is not essential.
Line 766: Line 766:


:EndNamespace
:EndNamespace
</source>
</syntaxhighlight>
}}
}}
== See also ==
== See also ==

Revision as of 21:51, 10 September 2022

To write bug-free code in a complex system and to forecast all errors is impossible. Therefore, implementing some kind of error trapping in an application which is supposed to run in a productive environment is a must. But to do this in a general and efficient manner is not easy. This article discusses techniques to solve this problem.

Introduction

Versions covered

The techniques we are going to discuss have been available in Dyalog APL for a long time. When you try to implement error trapping you should be very careful: it is easy to implement a non-interruptible loop. If this happens you have to kill the process in the task manager and the workspace is lost, so it is a good idea to save the workspace before you execute code after a change.

Considerations

When an application is executed in a production environment, error trapping can be used to solve the following goals:

  • Save a workspace as a snapshot reflecting the state of the application when the error appeared. This makes it easy to analyze a problem, and sometimes it is the only way to do so.
  • After having saved a snapshot it might be possible to restart the application. This might enable the user to run the application with different data, or to run different parts of the application on the same data.
  • Prevent the user from interrupting the application by pressing the keys the strong and the weak interrupt are associated with.
  • Continue execution in case a developer has forgotten to remove a stop vector.

When an application is started, it needs to be initialized. If an error occurs at this early stage, normally there is no way to recover. Once an application is fully initialized, it might be a good idea to try to restart it. However, if this procedure crashes itself, we must prevent an endless loop from generating tons of useless snapshot workspaces.

Preparation

It is good programming style to avoid using numbers in code. Instead of talking about 1001, for example, we should use a meaningful name:

<source lang=apl> ⎕CS 'Events' #.⎕NS ⎕FX 'r←StopVector' 'r←1001' ⎕FX 'r←WeakInterrupt' 'r←1002' ⎕FX 'r←StrongInterrupt' 'r←1003' </syntaxhighlight>

In a large system you want those to be constants, so a user cannot change them. That's why they are niladic functions.

We need also a user-defined event for restarting the application. This is explained soon:

<source lang=apl>

  1. .Events.RestartAppl←501

</syntaxhighlight>

According to the help file, users should use the range from 500 to 999 to define their own events.

Setting <source lang=apl inline>⎕TRAP</syntaxhighlight>

<source lang=apl inline>⎕TRAP</syntaxhighlight> allows us to implement a general mechanism on a global level. For discussion purposes let's assume the following:

  1. <source lang=apl inline>⎕LX</syntaxhighlight> is set to run function <source lang=apl inline>Run</syntaxhighlight>
  2. This function calls 3 sub-functions: <source lang=apl inline>Initial</syntaxhighlight>, <source lang=apl inline>Work</syntaxhighlight> and <source lang=apl inline>Shutdown</syntaxhighlight>
  3. <source lang=apl inline>Initial</syntaxhighlight> initializes the application: it opens files, interprets an INI file, takes the Windows registry into account, builds the GUI and so forth.
  4. <source lang=apl inline>Work</syntaxhighlight> simply runs <source lang=apl inline>⎕DQ</syntaxhighlight>
  5. <source lang=apl inline>Shutdown</syntaxhighlight> cleans up: it closes files, says good-by.

Solving the stop vector problem

Let's start with solving the stop vector problem: <source lang=apl> ⎕TRAP←⊂(#.Events.StopVector 'E' '→⎕LC') </syntaxhighlight> <source lang=apl inline>#.Events.StopVector</syntaxhighlight> returns 1001 which is the event number a stop vector is associated with. As soon as APL stops on a stop vector <source lang=apl inline>⎕EN</syntaxhighlight> is set to 1001. This event can be caught with <source lang=apl inline>⎕TRAP</syntaxhighlight>, so we can tell APL to execute (<source lang=apl inline>'E'</syntaxhighlight>) the expression given as third argument. In this case it tells APL to simply ignore stop vectors by resuming execution.

Preventing users from interrupting an application

The same technique can be used to prevent the user from interrupting the application, accidentally or purposely. Depending on the type of the application it might be a good idea to allow the user to interrupt the application by pressing either the key for the weak or the strong interrupt, to ask the user for confirmation and then to restart the application. This would allow the user to quit a lengthy operation that needs more time than expected.

Here, however, we will use this simple approach: <source lang=apl> events←#.Events.WeakInterrupt #.Events.StrongInterrupt ⎕TRAP,←⊂(events 'E' '→⎕LC') </syntaxhighlight>

Restarting the application

For reasons explained in a minute we now have to define the “Restart the application” procedure. For this, for the first time we do not use the <source lang=apl inline>'E'</syntaxhighlight> statement but the <source lang=apl inline>'C'</syntaxhighlight> statement. The C is short for Cut back. This instructs APL to cut the status indicator back to the level where <source lang=apl inline>⎕TRAP</syntaxhighlight> is localized – that is not necessarily where it was set – and execute the expression in the 3rd argument there. However, if <source lang=apl inline>⎕TRAP</syntaxhighlight> is not localized at all, i.e. it is in the workspace, the status indicator is cut back completely and the expression is executed in the workspace. <source lang=apl> ⎕TRAP,←⊂(#.Events.RestartAppl 'C' '→∆Restart') </syntaxhighlight> To make this work the function in which <source lang=apl inline>⎕TRAP</syntaxhighlight> is localized must have a label <source lang=apl inline>∆Restart</syntaxhighlight> or a function that returns a valid line number to branch to of course.

Catching Errors

If an unexpected error occurs, we want to execute a particular function to do the hard work. <source lang=apl> ⎕TRAP,←⊂((0 1000) 'E' '#.HandleError' ) </syntaxhighlight> The 0 stands for all the events from 1 to 999 while the 1000 stands for all events larger than 1000.

<source lang=apl inline>⎕TRAP</syntaxhighlight> may contain more than one error catching group. Since the contents of <source lang=apl inline>⎕TRAP</syntaxhighlight> is scanned from left to right, a statement will ONLY be executed for an event not processed earlier. That is the reason why we must define the restart event first.

For example, in the following statement: <source lang=apl> ⎕TRAP←(333 'E' 'expA') (0 'E' 'expB') </syntaxhighlight> event 333 will be caught by the 1st group and NOT by the 2nd even though 0 stands for “events from 1 to 999”. Only the expression <source lang=apl inline>expA</syntaxhighlight> will be executed.

The <source lang=apl inline>#.HandleError</syntaxhighlight> function

The <source lang=apl inline>#.HandleError</syntaxhighlight> function should do at least the following:

  • Perhaps neutralize <source lang=apl inline>⎕LX</syntaxhighlight>
  • Save the <source lang=apl inline>⎕DM</syntaxhighlight> and <source lang=apl inline>⎕EN</syntaxhighlight> settings
  • Save the snapshot
  • Maybe create an HTML page with general information about the error
  • Ask the user about a restart
  • Either try a restart of shut the application down

Misc

Developers and others

Of course the error trapping mechanism must distinguish between developers and others. Often it is good enough to check the APL version: use error trapping in case of runtime, otherwise not. If this is not possible, because some or all of the users are running the development version too, you can specify a parameter to tell the application that you are a developer. By default the application can then use error trapping.

Testing Error Trapping

Keep in mind that you want to have an easy opportunity to test the system with error trapping. So you may need another parameter that tells the system that error trapping has to be used. Last but not least, there should be an easy opportunity to let the application crash on purpose. I prefer to have a “developers menu”, which is displayed only to developers. Among other useful commands it offers a “Let's crash” option.

Control Structure <source lang=apl inline>:Trap</syntaxhighlight>

If you use <source lang=apl inline>:Trap</syntaxhighlight>, keep in mind that <source lang=apl inline>⎕TRAP</syntaxhighlight> and <source lang=apl inline>:Trap</syntaxhighlight> are both taken into account. That means that in case of <source lang=apl>

Trap 0

-'a'

Else

.

EndTrap

</syntaxhighlight>

the error caused by the <source lang=apl inline>-'a'</syntaxhighlight> statement is caught by the <source lang=apl inline>:Else</syntaxhighlight>, while the <source lang=apl inline>.</syntaxhighlight> is caught by the <source lang=apl inline>⎕TRAP</syntaxhighlight> setting.

When using <source lang=apl inline>:Trap</syntaxhighlight> try to be as specific as possible. For example, this code is faulty: <source lang=apl>

Trap 0

filename ⎕FTIE 0

else

filename ⎕FCREATE 0

EndTrap

</syntaxhighlight> because it tries to create a file not only if this file does not already exist but also if the current user lacks the right to tie it, for example because somebody else has already tied it exclusively. Therefore, it is a better to be specific: <source lang=apl>

Trap 22

filename ⎕FTIE 0

else

filename ⎕FCREATE 0

EndTrap

</syntaxhighlight> The best idea, however, is to use <source lang=apl inline>⎕NEXISTS</syntaxhighlight> to check the file for already being created. In general it is a good idea to use error trapping only for extraordinary problems.

The <source lang=apl inline>⎕SIGNAL</syntaxhighlight> system function

Note that an event which is <source lang=apl inline>⎕SIGNAL</syntaxhighlight>led can be intercepted with <source lang=apl inline>⎕TRAP</syntaxhighlight> but not <source lang=apl inline>:Trap</syntaxhighlight> If you execute this function: <source lang=apl> ∇test

⎕TRAP←501 'E' '⎕←caught by ⎕TRAP'
:Trap 501
    ⎕SIGNAL 501
:Else
    ⎕←'Caugth in :Else'
:EndTrap

∇ </syntaxhighlight> you get this: <source lang=apl> caught by ⎕TRAP </syntaxhighlight>

Ensure future trouble

A very easy way to create problems in the future is to do this: <source lang=apl>

Trap 0

DoSomethingHere

EndTrap

</syntaxhighlight> This technique is called “silent trapping”. If something is going wrong, do not take care and do not tell anybody about it!

Switching Error Trapping on or off

When you use error trapping, make sure that you can switch off error trapping on a general level. The easiest way to implement this idea is something like this:

<source lang=apl>

Trap #.ErrorTrapFlag/0

DoSomething

else

TakeCare EndTrap </syntaxhighlight> If the flag is true, error trapping is active, if not, the <source lang=apl inline>DoSomeThing</syntaxhighlight> statement will fail if an error occurs. This makes is much easier to debug an application.

You might need a more sophisticated mechanism for this, because under some circumstances you want to switch off most but not all error trapping statements. For example, if you use a logging mechanism which is logging every user action for analyzing purposes, the code doing this may cause an interrupt itself, for example because the disk is full which holds the logging files. In such a case it might be inappropriate that the logging code breaks the application. Therefore, you might control this code with <source lang=apl inline>:Trap</syntaxhighlight>-statements.

In such a case it might be a good idea to control the behavior of the application on different levels for code which is really essential in terms of business logic, for example, and for code which is not essential.

But even in such a case the problem should be communicated. I found the idea of a watchdog application very useful, which, among other tasks, is listening to UDP telegrams on a particular port. An application in trouble can then send a telegram to the watchdog, telling about the problem. Using a type of error class, the client can tell the watchdog about the seriousness of the problem, and the watchdog can then decide to simply display it on it's GUI or send a SMS message or/and an email to the admin.

Code

The below workspace contains all the code needed to implement the ideas discussed above.
⍴Parms←,Parms ⍝ Jump if even number of items
             R←                        ⍝ Structur invalid, get out!
         :Else
             Buffer←((⌊0.5×⍴Parms),2)⍴Parms ⍝ Build a matrix
             Buffer[;1]←' '~¨⍨ToUpper¨Buffer[;1]
             :If ~0∊⍴Allowed
:If (

See also

External links

Lessons

Documentation

APL development [edit]
Interface SessionTyping glyphs (on Linux) ∙ FontsText editors
Publications IntroductionsLearning resourcesSimple examplesAdvanced examplesMnemonicsISO 8485:1989ISO/IEC 13751:2001A Dictionary of APLCase studiesDocumentation suitesBooksPapersVideosAPL Quote QuadVector journalTerminology (Chinese, German) ∙ Neural networksError trapping with Dyalog APL (in forms)
Sharing code Backwards compatibilityAPLcartAPLTreeAPL-CationDfns workspaceTatinCider
Implementation ResourcesOpen-sourceMagic functionPerformanceAPL hardware
Developers Timeline of corporationsAPL2000DyalogIBMIPSASTSC