Silverfrost Forums

Welcome to our forums

Exception handling method for outside array limit

22 Jul 2014 1:00 #14347

Hello I am searching to identify the exception handling for catching the error in case I want to pass value into the array where it doesn't have its size. Pls. look at the below code:

program main integer, allocatable :: a(:) object('System.AccessViolationException') EXC1 allocate (a(1:5)) print*, 'program start' try do i = 1, 10 a(i) = i print*, 'i=',i,'a(i)=',a(i) enddo catch(EXC1) throw 'Something' print*, 'In catch block - insufficient array size to write' finally print*, 'In FINALLY BLOCK' endtry end program main

The output is given below:

program start i= 1a(i)= 1 i= 2a(i)= 2 i= 3a(i)= 3 i= 4a(i)= 4 i= 5a(i)= 5 i= 6a(i)= 6 i= 7a(i)= 7 i= 8a(i)= 8 i= 9a(i)= 9 i= 10a(i)= 10 In FINALLY BLOCK

This is not helping to catch the error to find out beyond a(5) it should throw error. the above object ('System.AccessViolationException') does not catch the error.

Any one can help to identify proper error handling method in 'System.????'. What is the reference should I make in Silverfrost FTN95 Express Visual compiler. Appreciate your help quickly. Thanks

22 Jul 2014 5:17 #14348

You have a few options. Compiling with /BOUNDS_CHECK will cause the program to stop and indicate where the exception occurs. However, you may wish to build checks into the code itself, in which case you could change your do loop to something like the following:

do i = 1, 10
if (i<=ubound(a),dim=1) then
a(i) = i
print*, 'i=',i,'a(i)=',a(i)
else
! perform some error handling here
endif
enddo 

That's not particularly efficient since the IF statement gets checked every time. Something like the following would be better:

do i = 1, ubound(a,dim=1)
a(i) = i
print*, 'i=',i,'a(i)=',a(i)
enddo 

[/code]

22 Jul 2014 5:33 #14349

Hi Simon Thank you very much for your suggestion. I will check. However, With /BOUNDS_CHECK, the code will stop at run time. But I want to handle all the errors during run time and continue the execution. For e.g. increasing the array size during run time, divide by zero, Data reading errors from text files etc. My objective is to throw the errors at user side for their action in some cases and in other cases, I need to handle them internally by continuing the application suitably. What is your suggestion in such situations.?

Thanks in advance..

Please login to reply.