From c74241a74a5c71cde6e68e426420fc4b951c643b Mon Sep 17 00:00:00 2001
From: Nicola Vigano <nicola.vigano@esrf.fr>
Date: Mon, 11 Jun 2012 13:24:34 +0000
Subject: [PATCH] Exceptions: added a type checker for the exception types.

It can even execute actions if a function handle is passed

Signed-off-by: Nicola Vigano <nicola.vigano@esrf.fr>

Conflicts:

	zUtil_EDF/edf_wait_read.m

git-svn-id: https://svn.code.sf.net/p/dct/code/trunk@493 4c865b51-4357-4376-afb4-474e03ccb993
---
 .../gtCopyCorrectUndistortCondor.m            |  2 +-
 1_preprocessing/gtSetup.m                     |  9 ++++----
 zUtil_EDF/edf_wait_read.m                     | 21 +++++++------------
 zUtil_ErrorHandling/gtCheckExceptionType.m    | 14 +++++++++++++
 zUtil_OAR/gt_mcc.m                            | 14 ++++++-------
 5 files changed, 34 insertions(+), 26 deletions(-)
 create mode 100644 zUtil_ErrorHandling/gtCheckExceptionType.m

diff --git a/1_preprocessing/gtCopyCorrectUndistortCondor.m b/1_preprocessing/gtCopyCorrectUndistortCondor.m
index 8f51ba8b..cb4e5cc2 100644
--- a/1_preprocessing/gtCopyCorrectUndistortCondor.m
+++ b/1_preprocessing/gtCopyCorrectUndistortCondor.m
@@ -156,7 +156,7 @@ while counter<100
                   gtCopy(fnamein, fnameout, fnameout_orig, parameters, move_data_flag);
                   found=1;
                 catch mexc
-                  if strfind(mexc.identifier, 'EDF:') == 1
+                  if gtCheckExceptionType(mexc, 'EDF:')
                     message = [ 'Skipping badly formed file: "' fnamein ...
                                 '" (' mexc.identifier ')'];
                     gtPrintException(mexc, message);
diff --git a/1_preprocessing/gtSetup.m b/1_preprocessing/gtSetup.m
index e64201bb..cda05175 100644
--- a/1_preprocessing/gtSetup.m
+++ b/1_preprocessing/gtSetup.m
@@ -233,9 +233,8 @@ xmlfname=fullfile(parameters.acq.collection_dir,[parameters.acq.name '.xml']);
 % try to read the .xml file that may be present
 try
     params_xml = gtLoadAcquisitionXML(xmlfname,interactive);
-catch Mexc
-    if strcmp(Mexc.identifier, 'XML:no_xml_file')
-        disp('Quitting..')
+catch mexc
+    if gtCheckExceptionType(mexc, 'XML:no_xml_file', @()disp('Quitting..'))
         return;
     end
 end
@@ -336,8 +335,8 @@ try
     if strcmpi(parameters.acq.type, '360degree')
         gtDBCreateSpotPairTable(parameters.acq.name, 0, 0);
     end
-catch Mexc
-    gtPrintException(Mexc, 'failed to set up tables! try again, forcing overwite of existing tables?')
+catch mexc
+    gtPrintException(mexc, 'failed to set up tables! try again, forcing overwite of existing tables?')
     check=inputwdefault('Overwrite tables? [y/n]', 'n');
     if strcmpi(check,'y')
         gtDBCreateDifblobTable(parameters.acq.name, 1);
diff --git a/zUtil_EDF/edf_wait_read.m b/zUtil_EDF/edf_wait_read.m
index 1321ecd2..a1237b31 100644
--- a/zUtil_EDF/edf_wait_read.m
+++ b/zUtil_EDF/edf_wait_read.m
@@ -61,12 +61,10 @@ function [img, varargout] = edf_wait_read(fname, bb, waitfortranslated)
           end
           pause(2)
         end
-      catch Mexc
-        gtPrintException(Mexc, 'EDF access failed.')
-      % just try again
-      %  if strfind(Mexc.identifier, 'EDF:') == 1
-      %    rethrow(Mexc)
-      %  end
+      catch mexc
+        gtPrintException(mexc, 'EDF access failed.')
+        % Just try again (we shouldn't rely on this)
+%         gtCheckExceptionType(mexc, 'EDF:', @()rethrow(mexc));
         disp(' - trying again')
         pause(1)
       end  %try - sometimes file acess fails because of simultaneous read / write access...
@@ -82,13 +80,10 @@ function [img, varargout] = edf_wait_read(fname, bb, waitfortranslated)
         img = edf_read(fname, bb ,'nodisp', info);
       end
       break
-    catch Mexc
-      gtPrintException(Mexc, 'EDF Read failed.')
-   % just try again
-   %   if strfind(Mexc.identifier, 'EDF:') == 1
-   %     rethrow(Mexc)
-   %   end
-      lasterr
+    catch mexc
+      gtPrintException(mexc, 'EDF Read failed.')
+      % Just try again (we shouldn't rely on this)
+%       gtCheckExceptionType(mexc, 'EDF:', @()rethrow(mexc));
       disp(' - trying again')
       pause(1)
     end
diff --git a/zUtil_ErrorHandling/gtCheckExceptionType.m b/zUtil_ErrorHandling/gtCheckExceptionType.m
new file mode 100644
index 00000000..ef43e54e
--- /dev/null
+++ b/zUtil_ErrorHandling/gtCheckExceptionType.m
@@ -0,0 +1,14 @@
+function got_it = gtCheckExceptionType(mexc, type, action)
+% GTCHECKEXCEPTIONTYPE Checks the type of the exception, and if requested
+%       performs an action.
+% Input:
+%       mexc - the exception
+%       type - the type to check (can be a regular expression)
+%       action - function handle to perform in case of match
+
+    got_it = ~isempty(regexp(mexc.identifier, type, 'once'));
+
+    if (got_it && exist('action', 'var') && isa(action, 'function_handle'))
+        action();
+    end
+end
diff --git a/zUtil_OAR/gt_mcc.m b/zUtil_OAR/gt_mcc.m
index 107e0693..d4fcc62a 100644
--- a/zUtil_OAR/gt_mcc.m
+++ b/zUtil_OAR/gt_mcc.m
@@ -7,8 +7,8 @@ try
     mcc_vers_minor = str2double(mcc_vers{1}{2});
     mcc_vers_major = str2double(mcc_vers{1}{1});
     compiled_fine = true; % In case of error, we will re modify it
-catch Mexc
-    gtPrintException(Mexc, 'Error: Is the Matlab Compiler installed?')
+catch mexc
+    gtPrintException(mexc, 'Error: Is the Matlab Compiler installed?')
     compiled_fine = false;
     return
 end
@@ -40,13 +40,13 @@ delete(filemask);
 fprintf('- Placing result in %s\n', path_for_binaries);
 try
     mcc('-m', myfunctionname, '-d', path_for_binaries);
-catch Mexc
-    if strfind(Mexc.identifier, 'mcc_err_checkout_failed')
+catch mexc
+    if gtCheckExceptionType(mexc, 'mcc_err_checkout_failed')
         disp('Could not get a compiler license.')
         disp('Surf to http://compweb/public/nice/matlab_licence.php')
         disp('and call the person with the compiler license to see if you could use it!')
     else
-        gtPrintException(Mexc);
+        gtPrintException(mexc);
     end
     compiled_fine = false;
     return
@@ -65,8 +65,8 @@ if ( mcc_vers_major < 4 || (mcc_vers_major == 4 && mcc_vers_minor < 8))
                     getenv('ARCH'), ctfname);
     try
         [status, result] = unix(cmd);
-    catch Mexc
-        gtPrintException(Mexc, 'Extraction of archive did not work')
+    catch mexc
+        gtPrintException(mexc, 'Extraction of archive did not work')
     end
 
     if status ~= 0
-- 
GitLab