Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error Handling Techniques

Status
Not open for further replies.

javijerome

Programmer
Jun 26, 2010
25
PH
Does fortran95 have some error handling techniques like the "try" statement of Visual Basic 2008?
 
Pure Fortran 95 does not have any form of exception handling. There was a proposal in the 1996 X3J3 draft for exception handling but I don't know if it got into 2003.

There are some partial 2003 compilers about but I don't know if there are any full 2003 implementations.
 
xwb, I think I read the other day somewhere that Cray and IBM compilers can handle the full F2003 set now. Slightly off-topic, but interesting nonetheless.

--------------------------------------
Background: Chemical engineer, familiar mostly with MATLAB, but now branching out into real programming.
 
I'm not sure, but I thing, there is no general exception handling concept in Fortran, like in OOP-languages:
try..catch (Java), try..except (Python), begin..resccue..end (Ruby)
In Fortran you can handle different exceptions at different ways - for example:
1. file exeption with iostat
Code:
  ...
  integer stat
  ...
  ! open file
  open (1, file='csv_file.csv', status='old', iostat=stat)
  if (stat .ne. 0) then
    write(*,*) 'File cannot be opened !'
    go to 99
  end if
  ...
  ! close file
  99 continue
  close(1)
2. Arithmetic exceptions with ieee_arithmetic module
Code:
[COLOR=#a020f0]program[/color] exception
[COLOR=#a020f0]use[/color] ieee_arithmetic
[COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]

[COLOR=#2e8b57][b]integer[/b][/color], [COLOR=#2e8b57][b]parameter[/b][/color] :: n[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]2[/color]
[COLOR=#2e8b57][b]integer[/b][/color] :: k
[COLOR=#2e8b57][b]real[/b][/color] :: x, y(n), z
x [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]1.0[/color]
y [COLOR=#804040][b]=[/b][/color] ([COLOR=#804040][b]/[/b][/color][COLOR=#ff00ff]5.0[/color], [COLOR=#ff00ff]0.0[/color][COLOR=#804040][b]/[/b][/color])
[COLOR=#804040][b]do[/b][/color] k [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]1[/color], n 
  z [COLOR=#804040][b]=[/b][/color] x[COLOR=#804040][b]/[/b][/color]y(k)
  [COLOR=#804040][b]if[/b][/color] (ieee_is_finite(z)) [COLOR=#804040][b]then[/b][/color]
    [COLOR=#804040][b]print[/b][/color] [COLOR=#804040][b]*[/b][/color], [COLOR=#ff00ff]"Z = "[/color], z
  [COLOR=#804040][b]else[/b][/color]
    [COLOR=#804040][b]print[/b][/color] [COLOR=#804040][b]*[/b][/color], [COLOR=#ff00ff]"Error: Z is infinite!"[/color]
  [COLOR=#804040][b]end if[/b][/color]
[COLOR=#804040][b]end do[/b][/color] 
[COLOR=#a020f0]end program[/color] exception
Code:
$ g95  exception.f95 -o exception

$ exception
 Z =  0.2
 Error: Z is infinite!
However, I don't know how wide ieee_arithmetic is supported by several compilers, because the above code doesn't compile with gfortran
Code:
$ gfortran  exception.f95 -o exception
exception.f95:2.19:

use ieee_arithmetic
                  1
Fatal Error: Can't open module file 'ieee_arithmetic.mod' for reading at (1): No such file or directory
gfortran.exe: Internal error: Aborted (program f951)
Please submit a full bug report.
See <[URL unfurl="true"]http://gcc.gnu.org/bugs.html>[/URL] for instructions.
 
ieee_arithmetic? was is it? silverfrost's fortran95 compiler brings an error. should i create a module first? or is this module supposed to be predefined?
 
See what mikrom said:

However, I don't know how wide ieee_arithmetic is supported by several compilers, because the above code doesn't compile with gfortran

Obviously, that isn't supported by Silverfrost's compiler if it gives you an error.

--------------------------------------
Background: Chemical engineer, familiar mostly with MATLAB, but now branching out into real programming.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top