diff --git a/include/dynamic-graph/tracer-real-time.h b/include/dynamic-graph/tracer-real-time.h
index 5792ffb4d990aab49892fc78233027b5ea447b4d..1dcf02ee9f2c8e1d509ce141d0bb75bde8d85234 100644
--- a/include/dynamic-graph/tracer-real-time.h
+++ b/include/dynamic-graph/tracer-real-time.h
@@ -50,17 +50,18 @@ public:
   DG_TRACERREALTIME_DLLAPI friend std::ostream &
   operator<<(std::ostream &os, const TracerRealTime &t);
 
-protected:
-  virtual void openFile(const SignalBase<int> &sig,
-                        const std::string &filename);
-
-  virtual void recordSignal(std::ostream &os, const SignalBase<int> &sig);
   void emptyBuffers();
 
   void setBufferSize(const int &SIZE) { bufferSize = SIZE; }
 
   const int &getBufferSize() { return bufferSize; }
 
+protected:
+  virtual void openFile(const SignalBase<int> &sig,
+                        const std::string &filename);
+
+  virtual void recordSignal(std::ostream &os, const SignalBase<int> &sig);
+
   typedef std::list<std::ofstream *> HardFileList;
   static const int BUFFER_SIZE_DEFAULT = 1048576; //  1Mo
 
diff --git a/src/traces/tracer-real-time.cpp b/src/traces/tracer-real-time.cpp
index e2fdcbeef553fdadd94241a125ff26c0d2e889a5..908442465b727cd692517b0dd1d5b32d03348eef 100644
--- a/src/traces/tracer-real-time.cpp
+++ b/src/traces/tracer-real-time.cpp
@@ -125,6 +125,11 @@ void TracerRealTime::openFile(const SignalBase<int> &sig,
   string filename = rootdir + basename + signame + suffix;
   dgDEBUG(5) << "Sig <" << sig.getName() << ">: new file " << filename << endl;
   std::ofstream *newfile = new std::ofstream(filename.c_str());
+  if (!newfile->good()) {
+    delete newfile;
+    DG_THROW ExceptionTraces(ExceptionTraces::NOT_OPEN,
+        "Could not open file " + filename + " for signal " + signame, "");
+  }
   dgDEBUG(5) << "Newfile:" << (void *)newfile << endl;
   hardFiles.push_back(newfile);
   dgDEBUG(5) << "Creating Outstringstream" << endl;
@@ -264,7 +269,7 @@ void TracerRealTime::recordSignal(std::ostream &os,
                 << "> " << endl;
 
   } catch (ExceptionAbstract &exc) {
-    throw exc;
+    throw;
   } catch (...) {
     DG_THROW ExceptionTraces(ExceptionTraces::NOT_OPEN,
                              "The buffer is not open", "");
diff --git a/src/traces/tracer.cpp b/src/traces/tracer.cpp
index 9f6dfb9470b33f655ab84a2cb6baa3b19b36efb3..99dadd76ab170331d523c683615e27abb5f16221 100644
--- a/src/traces/tracer.cpp
+++ b/src/traces/tracer.cpp
@@ -88,11 +88,12 @@ Tracer::Tracer(const std::string n)
 void Tracer::addSignalToTrace(const SignalBase<int> &sig,
                               const string &filename) {
   dgDEBUGIN(15);
+  // openFile may throw so it should be called first.
+  if (namesSet)
+    openFile(sig, filename);
   toTraceSignals.push_back(&sig);
   dgDEBUGF(15, "%p", &sig);
   names.push_back(filename);
-  if (namesSet)
-    openFile(sig, filename);
   triger.addDependency(sig);
   dgDEBUGOUT(15);
 }
diff --git a/tests/debug-real-time-tracer.cpp b/tests/debug-real-time-tracer.cpp
index 54c7a9893a1aa5328b41af499ac9c2e01f03b53e..b1de9dbdf1d1b0f142be02f996f229695dc8c1c3 100644
--- a/tests/debug-real-time-tracer.cpp
+++ b/tests/debug-real-time-tracer.cpp
@@ -65,37 +65,43 @@ BOOST_AUTO_TEST_CASE(test_tracer) {
   std::string basename("my-tracer");
   std::string suffix(".dat");
 
-  /// Test openfiles
-  atracer.openFiles(rootdir, basename, suffix);
+  atracer.setBufferSize(1<<14);
 
-  /// Add trace by name
+  // Check that an exception is thrown if the filename is invalid.
+  atracer.openFiles(rootdir, "invalid/filename", suffix);
+  BOOST_CHECK_THROW(atracer.addSignalToTraceByName("my-entity.out_double", "output"),
+      ExceptionTraces);
+
+  // Test openfiles
+  atracer.openFiles(rootdir, basename, suffix);
+  // Add trace by name
   atracer.addSignalToTraceByName("my-entity.out_double", "output");
 
   /// Add trace by name
-  SignalBase<int> &aSignal = entity.getSignal("out2double");
-
-  entity.m_sigdTwoTimeDepSOUT.recompute(2);
+  SignalBase<int> &out_double = entity.getSignal("out_double");
+  SignalBase<int> &out_double_2 = entity.getSignal("out2double");
 
-  Signal<double, int> &aSignalInt =
+  Signal<double, int> &in_double =
       *(dynamic_cast<Signal<double, int> *>(&entity.getSignal("in_double")));
 
-  aSignalInt.setConstant(1.5);
+  in_double.setConstant(1.5);
   atracer.start();
 
-  atracer.trace();
-
   std::string emptybuf_cmd_str("empty");
   command::Command *acmd = atracer.getNewStyleCommand(emptybuf_cmd_str);
   acmd->execute();
   for (int i = 0; i < 1000; i++) {
-    aSignal.setTime(i);
-    aSignalInt.setTime(i);
+    in_double.setTime(i);
+    out_double.recompute(i);
+    out_double_2.recompute(i);
     atracer.recordTrigger(i, i);
   }
   output_test_stream output;
+
   atracer.display(output);
 
   atracer.stop();
+  atracer.trace();
   atracer.clearSignalToTrace();
   atracer.closeFiles();
   acmd->execute();
@@ -105,5 +111,5 @@ BOOST_AUTO_TEST_CASE(test_tracer) {
       "TracerRealTime my-tracer [mode=play] : \n"
       "  - Dep list: \n"
       "     -> MyEntity(my-entity)::input(double)::out_double (in output)"
-      "	[0Mo/1Mo]	\n"));
+      "	[9Ko/16Ko]	\n"));
 }