 |
forums.silverfrost.com Welcome to the Silverfrost forums
|
View previous topic :: View next topic |
Author |
Message |
narayanamoorthy_k
Joined: 19 Jun 2014 Posts: 142 Location: Chennai, IN
|
Posted: Tue Jul 22, 2014 2:00 pm Post subject: Exception handling method for outside array limit |
|
|
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 _________________ Thanks and Regards
Moorthy |
|
Back to top |
|
 |
simon
Joined: 05 Jul 2006 Posts: 299
|
Posted: Tue Jul 22, 2014 6:17 pm Post subject: |
|
|
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:
Code: |
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:
Code: |
do i = 1, ubound(a,dim=1)
a(i) = i
print*, 'i=',i,'a(i)=',a(i)
enddo
|
[/code] |
|
Back to top |
|
 |
narayanamoorthy_k
Joined: 19 Jun 2014 Posts: 142 Location: Chennai, IN
|
Posted: Tue Jul 22, 2014 6:33 pm Post subject: |
|
|
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.. _________________ Thanks and Regards
Moorthy |
|
Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|