Is it possible handle exceptions in MQL5?

 

Hello folks, I have an script which call to a function which some times generates the exception:

Access violation read to 0xFFFFFFFFFFFFFFFF

It is not in our hands fix that because it is a RAM issue of MT5. With exactly the same parameters sometimes generates that exception.

So I need to handle this exception. I need something similar to the Java: try-catch block or whatever workaround.

To test the exception handling I think we can test with a call to the function:

void do_error() {
   int arr[] = {0, 1, 2};
   int index = 5;
   int error = arr[index];
}

for example:

int OnStart() {

   // I need to handle this exception ...
   do_error();
   //...

}

What I need is to detect the exception and don't break the execution flow of the script.


Thanks in advance, Cyberglassed.

 

There is no way to do that in mql5.

If there is a memory issue with MT5/mql5, you have to locate it and report to ServiceDesk.

 
cyberglassed:

Hello folks, I have an script which call to a function which some times generates the exception:

It is not in our hands fix that because it is a RAM issue of MT5. With exactly the same parameters sometimes generates that exception.

So I need to handle this exception. I need something similar to the Java: try-catch block or whatever workaround.

To test the exception handling I think we can test with a call to the function:

for example:

What I need is to detect the exception and don't break the execution flow of the script.


Thanks in advance, Cyberglassed.

In any other programming language you would get at out of range error. In fact, if you make the same mistake with buffers in MQL5 you also get an out of range error.

In this particular case, you can prevent it using the ArraySize() function. Like this.

void do_error() {
   int arr[] = {0, 1, 2};
   int index = 5;
   int size  = ArraySize(arr);
   int error;
   if(index < size)
   {
       int error = arr[index];
   } else {
       // Handle your error here
       Print("Out of range!");
   }
}

Based on this, you can create a multipurpose function that access an array and returns the desired position, or false as an "exception".

double ArrayGetValue(double &arr[], int index)
{
   int size  = ArraySize(arr);
   if(index < size)
   {
       return(arr[index]);
   } else {
       return(false); // False is the exception
   }
}

Reason: