NetCDF-C++  4.3.1-developer
ncCheck.cpp
1 #include <cstring>
2 #include "netcdf.h"
3 #include <ncException.h>
4 using namespace std;
5 using namespace netCDF::exceptions;
6 
7 // C++ API for netCDF4.
8 namespace netCDF
9 {
10  // function checks error code and if necessary throws appropriate exception.
11  void ncCheck(int retCode, const char* file, int line){
12  if (retCode==NC_NOERR)
13  return;
14 
15  const char* msg = 0;
16  if (NC_ISSYSERR(retCode)){
17  msg = std::strerror(retCode);
18  msg = msg ? msg : "Unknown system error";
19  }else{
20  msg = nc_strerror(retCode);
21  }
22 
23  switch(retCode) {
24  case NC_EBADID : throw NcBadId(msg,file,line);
25  case NC_ENFILE : throw NcNFile(msg,file,line);
26  case NC_EEXIST : throw NcExist(msg,file,line);
27  case NC_EINVAL : throw NcInvalidArg(msg,file,line);
28  case NC_EPERM : throw NcInvalidWrite(msg,file,line);
29  case NC_ENOTINDEFINE : throw NcNotInDefineMode(msg,file,line);
30  case NC_EINDEFINE : throw NcInDefineMode(msg,file,line);
31  case NC_EINVALCOORDS : throw NcInvalidCoords(msg,file,line);
32  case NC_EMAXDIMS : throw NcMaxDims(msg,file,line);
33  case NC_ENAMEINUSE : throw NcNameInUse(msg,file,line);
34  case NC_ENOTATT : throw NcNotAtt(msg,file,line);
35  case NC_EMAXATTS : throw NcMaxAtts(msg,file,line);
36  case NC_EBADTYPE : throw NcBadType(msg,file,line);
37  case NC_EBADDIM : throw NcBadDim(msg,file,line);
38  case NC_EUNLIMPOS : throw NcUnlimPos(msg,file,line);
39  case NC_EMAXVARS : throw NcMaxVars(msg,file,line);
40  case NC_ENOTVAR : throw NcNotVar(msg,file,line);
41  case NC_EGLOBAL : throw NcGlobal(msg,file,line);
42  case NC_ENOTNC : throw NcNotNCF(msg,file,line);
43  case NC_ESTS : throw NcSts(msg,file,line);
44  case NC_EMAXNAME : throw NcMaxName(msg,file,line);
45  case NC_EUNLIMIT : throw NcUnlimit(msg,file,line);
46  case NC_ENORECVARS : throw NcNoRecVars(msg,file,line);
47  case NC_ECHAR : throw NcChar(msg,file,line);
48  case NC_EEDGE : throw NcEdge(msg,file,line);
49  case NC_ESTRIDE : throw NcStride(msg,file,line);
50  case NC_EBADNAME : throw NcBadName(msg,file,line);
51  case NC_ERANGE : throw NcRange(msg,file,line);
52  case NC_ENOMEM : throw NcNoMem(msg,file,line);
53  case NC_EVARSIZE : throw NcVarSize(msg,file,line);
54  case NC_EDIMSIZE : throw NcDimSize(msg,file,line);
55  case NC_ETRUNC : throw NcTrunc(msg,file,line);
56 
57  // The following are specific netCDF4 errors.
58  case NC_EHDFERR : throw NcHdfErr(msg,file,line);
59  case NC_ECANTREAD : throw NcCantRead(msg,file,line);
60  case NC_ECANTWRITE : throw NcCantWrite(msg,file,line);
61  case NC_ECANTCREATE : throw NcCantCreate(msg,file,line);
62  case NC_EFILEMETA : throw NcFileMeta(msg,file,line);
63  case NC_EDIMMETA : throw NcDimMeta(msg,file,line);
64  case NC_EATTMETA : throw NcAttMeta(msg,file,line);
65  case NC_EVARMETA : throw NcVarMeta(msg,file,line);
66  case NC_ENOCOMPOUND : throw NcNoCompound(msg,file,line);
67  case NC_EATTEXISTS : throw NcAttExists(msg,file,line);
68  case NC_ENOTNC4 : throw NcNotNc4(msg,file,line);
69  case NC_ESTRICTNC3 : throw NcStrictNc3(msg,file,line);
70  case NC_EBADGRPID : throw NcBadGroupId(msg,file,line);
71  case NC_EBADTYPID : throw NcBadTypeId(msg,file,line); // netcdf.h file inconsistent with documentation!!
72  case NC_EBADFIELD : throw NcBadFieldId(msg,file,line); // netcdf.h file inconsistent with documentation!!
73  // case NC_EUNKNAME : throw NcUnkownName("Cannot find the field id.",file,line); // netcdf.h file inconsistent with documentation!!
74 
75  case NC_ENOGRP : throw NcEnoGrp(msg,file,line);
76  case NC_ELATEDEF : throw NcElateDef(msg,file,line);
77 
78  default:
79  throw NcException(retCode, msg, file, line);
80  }
81  }
82 
83  void ncCheckDefineMode(int ncid)
84  {
85  int status = nc_redef(ncid);
86  if (status != NC_EINDEFINE) ncCheck(status, __FILE__, __LINE__);
87  }
88 
89  void ncCheckDataMode(int ncid)
90  {
91  int status = nc_enddef(ncid);
92  if (status != NC_ENOTINDEFINE) ncCheck(status, __FILE__, __LINE__);
93  }
94 }
Thrown if cannot write.
Definition: ncException.h:285
Thrown if NC_MAX_DIMS is exceeded.
Definition: ncException.h:103
Class represents a netCDF atomic Char type.
Definition: ncChar.h:10
Thrown if NC_UNLIMITED size is already in use.
Definition: ncException.h:194
Thrown if bad field id.
Definition: ncException.h:369
Thrown if attempting netcdf-4 operation on strict nc3 netcdf-4 file.
Definition: ncException.h:348
Thrown if cannot return a netCDF group.
Definition: ncException.h:383
Thrown if bad group id.
Definition: ncException.h:355
Thrown if, having set NC_NOCLOBBER, the specified dataset already exists.
Definition: ncException.h:55
Thrown if invalid dimension size.
Definition: ncException.h:257
Thrown if operation not allowed in data mode.
Definition: ncException.h:76
Thrown if one or more variable sizes violate format constraints.
Definition: ncException.h:250
C++ API for netCDF4.
Definition: ncAtt.h:9
Thrown if not a netCDF file.
Definition: ncException.h:173
Thrown if the action is prohibited on the NC_GLOBAL varid.
Definition: ncException.h:166
Thrown if in FORTRAN, string is too short.
Definition: ncException.h:180
Thrown if attempting netcdf-4 operation on netcdf-3 file.
Definition: ncException.h:341
Thrown if dim meta.
Definition: ncException.h:306
Thrown if not a valid netCDF data type.
Definition: ncException.h:131
Thrown if string match to name is in use.
Definition: ncException.h:110
Thrown if NC_MAX_VARS is exceeded.
Definition: ncException.h:152
Thrown if attribute is not found.
Definition: ncException.h:117
Thrown if the specified netCDF ID does not refer to an open netCDF dataset.
Definition: ncException.h:41
Thrown if not a netCDF id.
Definition: ncException.h:62
Thrown if an error was reported by the HDF5 layer.
Definition: ncException.h:271
Thrown if too many netcdf files are open.
Definition: ncException.h:48
Thrown if file meta.
Definition: ncException.h:299
Thrown if edge+start exceeds dimension bound.
Definition: ncException.h:215
Thrown if memory allocation (malloc) failure.
Definition: ncException.h:243
Exception classes.
Definition: ncException.h:15
Thrown if cannot create.
Definition: ncException.h:292
Thrown if attribute meta.
Definition: ncException.h:313
Thrown if cannot read.
Definition: ncException.h:278
Thrown if operation not allowed in defined mode.
Definition: ncException.h:83
Base object is thrown if a netCDF exception is encountered.
Definition: ncException.h:24
Thrown if illegal stride.
Definition: ncException.h:222
void ncCheckDataMode(int ncid)
Function checks if the file (group) is in data mode.
Definition: ncCheck.cpp:89
Thrown if NC_MAX_NAME is exceeded.
Definition: ncException.h:187
void ncCheck(int retCode, const char *file, int line)
Function checks error code and if necessary throws an exception.
Definition: ncCheck.cpp:11
Index exceeds dimension bound.
Definition: ncException.h:96
Thrown if Nc_MAX_ATTRS is exceeded.
Definition: ncException.h:124
Thrown if an operation to set the chunking, endianness, fill of a NcVar object is issued after a call...
Definition: ncException.h:426
void ncCheckDefineMode(int ncid)
Function checks if the file (group) is in define mode.
Definition: ncCheck.cpp:83
Thrown if attribute exists.
Definition: ncException.h:334
Thrown if math result not representable.
Definition: ncException.h:236
Thrown if bad type id.
Definition: ncException.h:362
Thrown if file likely truncated or possibly corrupted.
Definition: ncException.h:264
Thrown if Nc_UNLIMITED is in the wrong index.
Definition: ncException.h:145
Thrown if no compound.
Definition: ncException.h:327
Thrown if nc_rec op when there are no record vars.
Definition: ncException.h:201
Thrown if invalid argument.
Definition: ncException.h:69
Thrown if attribute or variable name contains illegal characters.
Definition: ncException.h:229
Thrown if variable is not found.
Definition: ncException.h:159
Thrown if an invalid dimension id or name.
Definition: ncException.h:138
Thrown if variable meta.
Definition: ncException.h:320

Return to the Main Unidata NetCDF page.
Generated on Fri Nov 11 2016 15:28:29 for NetCDF-C++. NetCDF is a Unidata library.