简体   繁体   English

fortran子例程的Qt错误处理

[英]Qt error handling for fortran subroutines

I have a Qt GUI project that calls a fortran subroutine from a C++ function. 我有一个Qt GUI项目,该项目从C ++函数调用fortran子例程。 This fortran subroutine reads some data from a text file. 此fortran子例程从文本文件读取一些数据。 But whenever the fortran code fails (eg bad integer in item list etc), the GUI window automatically closes. 但是,只要fortran代码失败(例如,项目列表中的错误整数等),GUI窗口就会自动关闭。 Is there a way to prevent it from closing if an error occures? 如果发生错误,是否有办法防止它关闭?

I tried using try-catch blocks like this 我尝试使用try-catch块

 try
 {
  // fortran function call
  test_();
 }
 catch(...)
 {
  qDebug()<<"Error";
 }

but it didn't work. 但这没用。 Throwing exceptions with the throw statement does work however. 但是,throw语句的throwing异常确实可以工作。

I also tried to subclass QApplication class and reimplement the notify() function: 我还尝试子类化QApplication类并重新实现notify()函数:

   bool SafeApp::notify(QObject* obj,QEvent* event)
   {
   try
    {
     return QApplication::notify(obj,event);
    }
   catch(std::exception& e)
    {
     return false;
    }
   }

The code compiles and runs fine, but it still crashes after calling the fortran function. 代码可以编译并正常运行,但是在调用fortran函数后仍然崩溃。

Fortran does not have exceptions, so the Fortran function you're calling is not throwing anything that C++ can catch. Fortran没有例外,因此您正在调用的Fortran函数不会抛出C ++可以捕获的任何东西。

OTOH, in Fortran the default behavior for errors such as I/O errors is to stop the program. OTOH,在Fortran中,错误(例如I / O错误)的默认行为是停止程序。 The GFortran runtime library uses the libc exit() or abort() functions for this, depending on what kind of error is encountered. GFortran运行时库为此使用libc exit()或abort()函数,具体取决于遇到哪种错误。

What you might be able to do, is to add IOSTAT= and/or IOMSG= specifiers to the Fortran I/O statements. 您可能能够做的就是将IOSTAT =和/或IOMSG =说明符添加到Fortran I / O语句中。 In that case errors will cause a nonzero iostat and some message in the string pointed to by IOMSG rather than stopping the program. 在这种情况下,错误将导致iostat非零,并在IOMSG指向的字符串中显示某些消息,而不是停止程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM