Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Error handling 如何正确实现错误处理?_Error Handling_Idl Programming Language - Fatal编程技术网

Error handling 如何正确实现错误处理?

Error handling 如何正确实现错误处理?,error-handling,idl-programming-language,Error Handling,Idl Programming Language,我正在调用一个函数fit_circle,该函数可能调用也可能不调用消息过程fit\u circle调用一个函数poly\u fit,该函数可能调用消息过程,但有时也会产生一个数学错误(对此我无能为力) 是否有可能构造一个错误处理程序来处理所有这些场景而不返回交互式提示?我尝试了以下方法: FUNCTION arb_funct,circular_data CATCH, ERROR IF ERROR NE 0 THEN BEGIN CATCH, /CANCEL

我正在调用一个函数
fit_circle
,该函数可能调用也可能不调用
消息
过程
fit\u circle
调用一个函数
poly\u fit
,该函数可能调用
消息
过程,但有时也会产生一个数学错误(对此我无能为力)

是否有可能构造一个错误处理程序来处理所有这些场景而不返回交互式提示?我尝试了以下方法:

FUNCTION arb_funct,circular_data
    CATCH, ERROR
    IF ERROR NE 0 THEN BEGIN
        CATCH, /CANCEL
        print, 'An Error Occured'
        return, []
    ENDIF

    circle_fit_result = fit_circle(circular_data)
    return, circle_fit_result
END

但是,错误处理程序从不触发。唯一的问题似乎是寻找过程中定义的错误处理程序。

对我来说很有效。下面是我的示例代码:

pro mg_error_demo_helper2
  compile_opt strictarr

  a = b
end


pro mg_error_demo_helper1
  compile_opt strictarr

  mg_error_demo_helper2
end


pro mg_error_demo
  compile_opt strictarr
  catch, error
  if (error ne 0L) then begin
    catch, /cancel
    print, 'An error occurred'
    return
  endif

  mg_error_demo_helper1
end
当我运行它时,我得到:

IDL> mg_error_demo
% Compiled module: MG_ERROR_DEMO.
error happened

下面是我提到的一个例子:

FUNCTION some_function

;;--------------------------------------------------------------------------
;;  Error handling [put towards beginning of routine]
;;--------------------------------------------------------------------------
CATCH, error_status
IF (error_status NE 0) THEN BEGIN
  ;;  Cancel error handler
  CATCH, /CANCEL
  ;;  Print error message
  PRINT, 'Error index: ', error_status
  PRINT, 'Error message: ', !ERROR_STATE.MSG
  ;;  Define return variable or how to handle error
  ;;    --> I usually use a formatted output consistent with the expected
  ;;        output, but here I will just use 0 for example.
  dumb           = 0  
  ;;  Return defined variable
  RETURN,dumb
ENDIF

;;--------------------------------------------------------------------------
;;  Main body of routine
;;--------------------------------------------------------------------------
blah = 0
; blah
; blah
; blah

;;--------------------------------------------------------------------------
;;  Return to user
;;--------------------------------------------------------------------------

RETURN,return_variable
END

希望有帮助……

IDL有三种不同类型的“可捕获”错误:

  • “正常”错误:这些是最常见的要处理的错误类型。调用
    消息
    时会抛出它们。使用
    catch
    on\u error
    来处理它们
  • IO错误:这些错误发生在转换、磁盘读取、内存或其他“IO”错误发生时。通常,这些也会被
    catch
    on\u error
    捕获。如果没有,请使用ioerror上的
    来处理它们
  • 数学错误:当你除以0,用NaN或无穷大进行某些数学运算时,就会发生这些错误。请使用
    检查\u Math
    处理它们!除了
下面是一个使用所有三合一程序的示例:

function arb_funct, circular_data

    ; handle normal errors
    catch, error
    if error ne 0 then begin
        catch, /cancel
        print, 'A normal error occured: ' + !error_state.msg
        return, []
    endif

    ; handle IO errors
    on_ioerror, arb_funct__ioerror
    if 0B then begin
        arb_funct__ioerror:
        print, 'An IO error occured: ' + !error_state.msg
        return, []
    endif

    ; clear math errors from previous routines
    math_err = check_math(/print)
    previous_except = !except
    !except = 0

    ; do stuff
    circle_fit_result = fit_circle(circular_data)

    ; handle math errors
    math_err = check_math()
    !except = previous_except
    if math_err ne 0 then begin
        print, 'A math error occured: ' + strtrim(math_err, 1)
        return, []
    endif

    ; and finally return
    return, circle_fit_result
end

我认为问题在于我使用的是一个函数,而不是一个过程。遗憾的是,我无法控制这一点。@mgalloy-您是如何获得错误消息的?我在你的代码中看不到这一点。我遗漏了什么吗?@maxf130-我在函数中使用了CATCH语句,所以这不是问题。您只需要为RETURN语句提供一个值。函数或过程,这并不重要。我收到消息“错误发生了”,请参阅我答案的最后一行。能否显示
FIT_CIRCLE
的代码?能否添加程序无法运行时显示的堆栈跟踪(错误消息)?