LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Can I hide the error message (throw an exception)?

My codes are:
 
 FILE *ptr;
 
 ptr  = fopen (str_file, "r");
 
 if(ptr == NULL) // error happen
 {
        clearerr (ptr);
        MessagePopup ("File Open error", "Error");
 }
*) But when the file does not exist, the message shows before the program check ptr == NULL ... can I hide that message? I understand that CVI not support try & catch! But there is another way?
 
Thanks for any help
0 Kudos
Message 1 of 5
(3,935 Views)

Use you use SetBreakOnLibraryErrors to handle the error yourself instead of having CVI pop up a error box.

 int bPrevBreak;
 FILE *ptr;
 
 // ignore library errors opening a file: code will handle errors
 bPrevBreak = SetBreakOnLibraryErrors (FALSE);
 ptr  = fopen (str_file, "r");
 SetBreakOnLibraryErrors (bPrevBreak); // restore previous break condition
 
 if(ptr == NULL) // error happen
     // handle the error

else

// do something else

0 Kudos
Message 2 of 5
(3,932 Views)

It works beautifully, by the way how can we verify the error info.?

Many thanks Smiley Happy

0 Kudos
Message 3 of 5
(3,913 Views)

fopen sets a global named errno.  You don't have to declare errno.  It's declared in errno.h which is included in ansi_c.h which you need to #include in order to use fopen().

If you have the Windows SDK installed, you can use FormatMessage to get the error message.  See the example I posted here: http://forums.ni.com/ni/board/message?board.id=180&message.id=12548&query.id=112013#M12548

Here's the meat of it:

#include <ansi_c.h>
#include <windows.h>
#include <utility.h>

main()
{

char errorMsg[256];
int prevBreak;
FILE *fp;

// disable break on library errors
prevBreak = SetBreakOnLibraryErrors (0);

// try opening a non-existent file
if ((fp = fopen ("ThisFileDoesn'tExist.txt", "r")) == NULL)
{
 // get the error message from the errno set by fopen
 FormatMessage(
     FORMAT_MESSAGE_FROM_SYSTEM,
     NULL,
     errno,
     LANG_NEUTRAL,
     errorMsg,
     256,
     NULL);
 // display the error in a messagebox
 MessageBox( NULL,
    errorMsg, "Error opening file",
    MB_APPLMODAL | MB_ICONWARNING);
}
// restore previous break on library errors state
SetBreakOnLibraryErrors (prevBreak);
}

 

If you want to check the errno yourself, look at the errors defined in errno.h, then try something like this.

 if ((fpOutputFile = fopen (outputFileName, "w")) == NULL)
 {
  switch (errno)
  {
   case ENOENT:
    strcpy (errMsg1, "No such file or directory.");
    break;
   case EACCES:
    strcpy (errMsg1, "Access denied.  File may be already open.");
    break;
   case EIO:
    strcpy (errMsg1, "I/O error.");
    break;
   case ENOMEM:
    strcpy (errMsg1, "Insufficient memory.");
    break;
   case EMFILE:
    strcpy (errMsg1, "Too many open files.");
    break;
   case ENOSPC:
    strcpy (errMsg1, "No space left on device.");
    break;
   default:
    sprintf(errMsg1, "Error number %d.", errno);
  }
  sprintf(errMsg, "Error opening file %s\n%s\n"
    "Click OK to try default path.",
    outputFileName, errMsg1);
  MessagePopup ("File Error", errMsg);

   return 0;
  }

0 Kudos
Message 4 of 5
(3,902 Views)

It is very really helpfull!

Many thanks again  

0 Kudos
Message 5 of 5
(3,880 Views)