3.15 Exception Handling
CRS can handle exceptions with try… catch , much like JavaScript.
try… catch throw statement
try {
Execution statement 1
}
catch (e) {
Execution statement 2
}
Execution statement 1 enclosed in a try block is executed, and if an exception occurs in the execution process, the statement execution is stopped in the subsequent execution statement 1 and control is transferred to execution statement 2.
The formal argument e of the catch statement is passed an Exception object. You can see what kind of exception occurred by the property of the Exception object.
e.Message Error message
e.Method Function classification that caused the exception
e.Code Exception code
Example
try {
var a = foo1 (1);
var b = foo2 (2);
var c = foo3 (3);
}
catch (e) {
print (" Exception ", e.Message, " occurred \ n");
}
Within the catch block, you can throw the exception received by catch to a higher block by executing a throw statement with no arguments or by throwing the Exception object received by the catch statement.
try {
var a = foo1 (1);
var b = foo2 (2);
var c = foo3 (3);
}
catch (e) {
if (e.Method == "DOM") {
print ("DOM exception ", e.Message, " occurred \ n");
} else {
throw e;
}
}
try… catch scope
Exception handling with try… catch is only valid at run time. Note that even if you put the entire CRS file generated by Biz / Designer in a try block, you will not be able to catch the exception caused by the execution of the event handler.
try { ← try block start
Form form1 {
Width = 100;
Height = 100;
::
Function OnTouch (e) {
::
throw new Exception ("app", 1, "test");
::
}
}
} ← end of try block
catch (e) {
print (e.Message, "\ n");
}
In this example, the entire Form is put in a try block, but this try block catches the exception that occurs when the form is generated, but it exits the try block when the form is generated, so it is inside the OnTouch event handler. The exceptions that occur in are not catchable.
To catch the exception that occurs in OnTouch , write as follows.
Form form1 {
Width = 100;
Height = 100;
::
Function OnTouch (e) {
try { ← try block start
::
throw new Exception ("app", 1, "test");
::
} ← end of try block
catch (e) {
print (e.Message, "\ n");
}
}
}
User-defined exception
You can raise an application-specific exception by creating an Exception object and passing it to the throw statement.
Example
Function foo (a) {
if (a == 0) {
throw new Exception ("app", 5, " argument is 0 ") ;
}
return 100 / a;
}
Pass the following values to the constructor of the Exception object.
First parameter: Specifies the type of exception. It is the value of the Method property.
Second parameter: Specify the exception number . It is the value of the Code property.
Third parameter : Specify a descriptive text. It is the value of the Message property.
Global exception handler
Added from Ver.4.0.0 and Mobile Ver.3.0.0
It is an error handling mechanism extended from Biz / Browser ver 4.0.0 . By defining the OnException function in “//” ( Root object), all uncaught exceptions will be caught and called.
:
// {
Function OnException (e) {
MessageBox (" An error has occurred. Please contact your administrator ");
Login ("http://server/startup.crs");
return false;
}
}
:
The OnException handler is a feature provided to provide an application-specific error reporting tool that normally reports an error to the user and exits. When control is passed to the OnException handler, all the call stacks that are being executed are released, so it is not possible to continue processing the script that caused the error.
Also, if another exception occurs while processing OnException , the error handler built into Biz / Browser is executed.
If the OnException function returns true , the CRS interpreter will continue processing while preserving the session. If it returns anything other than true , the CRS interpreter resets the session and stops.
Exceptions that can continue processing should be caught by try … catch in each processing so that control is not passed to OnException . When OnException is executed, the position and contents of the exception cannot be specified, so there is a possibility that the screen is not displayed normally or the object generation is stopped in the middle of the process. there is. After reporting an error, OnException recommends that you stop processing or use the Login () method to reset the session and restart the application.