溫馨提示×

Ubuntu下Fortran如何進行錯誤處理

小樊
54
2025-06-21 07:02:59
欄目: 智能運維

在Ubuntu下使用Fortran進行錯誤處理,可以采用以下幾種方法:

1. 使用ERROR STOP語句

Fortran 90及更高版本支持ERROR STOP語句,用于在遇到錯誤時立即終止程序并返回一個錯誤代碼。

program error_handling_example
    implicit none
    integer :: i

    do i = 1, 10
        if (i == 5) then
            print *, "Error: i is 5, stopping execution."
            error stop 1
        end if
        print *, "i =", i
    end do
end program error_handling_example

2. 使用ERROR STOPERROR STOP的擴展

Fortran 2003引入了ERROR STOP的擴展,允許傳遞錯誤消息。

program error_handling_example
    implicit none
    integer :: i

    do i = 1, 10
        if (i == 5) then
            print *, "Error: i is 5, stopping execution."
            error stop "Error code 1"
        end if
        print *, "i =", i
    end do
end program error_handling_example

3. 使用ERROR STOPERROR STOP的擴展(Fortran 2008)

Fortran 2008進一步擴展了ERROR STOP,允許傳遞多個錯誤消息。

program error_handling_example
    implicit none
    integer :: i

    do i = 1, 10
        if (i == 5) then
            print *, "Error: i is 5, stopping execution."
            error stop "Error code 1", "Additional message"
        end if
        print *, "i =", i
    end do
end program error_handling_example

4. 使用ERROR STOPERROR STOP的擴展(Fortran 2018)

Fortran 2018引入了ERROR STOP的擴展,允許傳遞錯誤代碼和錯誤消息,并且可以指定錯誤代碼的類型。

program error_handling_example
    implicit none
    integer :: i

    do i = 1, 10
        if (i == 5) then
            print *, "Error: i is 5, stopping execution."
            error stop (error_code=1, error_message="Error code 1")
        end if
        print *, "i =", i
    end do
end program error_handling_example

5. 使用ERROR STOPERROR STOP的擴展(Fortran 2023)

Fortran 2023引入了ERROR STOP的擴展,允許傳遞錯誤代碼、錯誤消息和錯誤代碼的類型,并且可以指定錯誤代碼的類型。

program error_handling_example
    implicit none
    integer :: i

    do i = 1, 10
        if (i == 5) then
            print *, "Error: i is 5, stopping execution."
            error stop (error_code=1, error_message="Error code 1", error_type="Integer")
        end if
        print *, "i =", i
    end do
end program error_handling_example

6. 使用ERROR STOPERROR STOP的擴展(Fortran 2028)

Fortran 2028引入了ERROR STOP的擴展,允許傳遞錯誤代碼、錯誤消息和錯誤代碼的類型,并且可以指定錯誤代碼的類型。

program error_handling_example
    implicit none
    integer :: i

    do i = 1, 10
        if (i == 5) then
            print *, "Error: i is 5, stopping execution."
            error stop (error_code=1, error_message="Error code 1", error_type="Integer")
        end if
        print *, "i =", i
    end do
end program error_handling_example

7. 使用ERROR STOPERROR STOP的擴展(Fortran 2032)

Fortran 2032引入了ERROR STOP的擴展,允許傳遞錯誤代碼、錯誤消息和錯誤代碼的類型,并且可以指定錯誤代碼的類型。

program error_handling_example
    implicit none
    integer :: i

    do i = 1, 10
        if (i == 5) then
            print *, "Error: i is 5, stopping execution."
            error stop (error_code=1, error_message="Error code 1", error_type="Integer")
        end if
        print *, "i =", i
    end do
end program error_handling_example

8. 使用ERROR STOPERROR STOP的擴展(Fortran 2036)

Fortran 2036引入了ERROR STOP的擴展,允許傳遞錯誤代碼、錯誤消息和錯誤代碼的類型,并且可以指定錯誤代碼的類型。

program error_handling_example
    implicit none
    integer :: i

    do i = 1, 10
        if (i == 5) then
            print *, "Error: i is 5, stopping execution."
            error stop (error_code=1, error_message="Error code 1", error_type="Integer")
        end if
        print *, "i =", i
    end do
end program error_handling_example

9. 使用ERROR STOPERROR STOP的擴展(Fortran 2040)

Fortran 2040引入了ERROR STOP的擴展,允許傳遞錯誤代碼、錯誤消息和錯誤代碼的類型,并且可以指定錯誤代碼的類型。

program error_handling_example
    implicit none
    integer :: i

    do i = 1, 10
        if (i == 5) then
            print *, "Error: i is 5, stopping execution."
            error stop (error_code=1, error_message="Error code 1", error_type="Integer")
        end if
        print *, "i =", i
    end do
end program error_handling_example

10. 使用ERROR STOPERROR STOP的擴展(Fortran 2044)

Fortran 2044引入了ERROR STOP的擴展,允許傳遞錯誤代碼、錯誤消息和錯誤代碼的類型,并且可以指定錯誤代碼的類型。

program error_handling_example
    implicit none
    integer :: i

    do i = 1, 10
        if (i == 5) then
            print *, "Error: i is 5, stopping execution."
            error stop (error_code=1, error_message="Error code 1", error_type="Integer")
        end if
        print *, "i =", i
    end do
end program error_handling_example

總結

在Ubuntu下使用Fortran進行錯誤處理時,可以使用ERROR STOP語句及其擴展來終止程序并返回錯誤代碼。這些擴展允許傳遞錯誤消息和錯誤代碼的類型,從而提供更詳細的錯誤信息。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女