diff --git a/src/tools/qml/qmlobserver/jsdebuggeragent.cpp b/src/tools/qml/qmlobserver/jsdebuggeragent.cpp
index 9d3dbec225e0520c4677cedebb3c6e3d6a4f6d67..e6007676e990f1b1be57fec4eb8d15c166fe957c 100644
--- a/src/tools/qml/qmlobserver/jsdebuggeragent.cpp
+++ b/src/tools/qml/qmlobserver/jsdebuggeragent.cpp
@@ -52,6 +52,26 @@
 
 QT_BEGIN_NAMESPACE
 
+class JSDebuggerAgent::SetupExecEnv {
+    JSDebuggerAgent* agent;
+    JSDebuggerAgent::State previousState;
+    bool hadException;
+public:
+    SetupExecEnv(JSDebuggerAgent *a)
+        : agent(a),
+          previousState(a->state),
+          hadException(a->engine()->hasUncaughtException())
+        {
+            agent->state = JSDebuggerAgent::Stopped;
+        }
+
+    ~SetupExecEnv() {
+        if (!hadException && agent->engine()->hasUncaughtException())
+            agent->engine()->clearExceptions();
+        agent->state = previousState;
+    }
+};
+
 class JSAgentWatchData {
 public:
     QByteArray exp;
@@ -323,25 +343,22 @@ void JSDebuggerAgent::messageReceived(const QByteArray& message)
         state = NoState;
         continueExec();
     } else if (command == "EXEC") {
-        State oldState = state;
-        state = Stopped;
+        SetupExecEnv execEnv(this);
+
         QByteArray id;
         QString expr;
         ds >> id >> expr;
 
         JSAgentWatchData data = JSAgentWatchData::fromScriptValue(expr, engine()->evaluate(expr));
         knownObjectIds << data.objectId;
-        // Clear any exceptions occurred during locals evaluation.
-        engine()->clearExceptions();
 
         QByteArray reply;
         QDataStream rs(&reply, QIODevice::WriteOnly);
         rs << QByteArray("RESULT") << id << data;
         sendMessage(reply);
-        state = oldState;
     } else if (command == "EXPAND") {
-        State oldState = state;
-        state = Stopped;
+        SetupExecEnv execEnv(this);
+
         QByteArray requestId;
         quint64 objectId;
         ds >> requestId >> objectId;
@@ -352,17 +369,13 @@ void JSDebuggerAgent::messageReceived(const QByteArray& message)
         QList<JSAgentWatchData> result = expandObject(v);
         recordKnownObjects(result);
 
-        // Clear any exceptions occurred during locals evaluation.
-        engine()->clearExceptions();
-
         QByteArray reply;
         QDataStream rs(&reply, QIODevice::WriteOnly);
         rs << QByteArray("EXPANDED") << requestId << result;
         sendMessage(reply);
-        state = oldState;
+
     } else if (command == "ACTIVATE_FRAME") {
-        State oldState = state;
-        state = Stopped;
+        SetupExecEnv execEnv(this);
 
         int frameId;
         ds >> frameId;
@@ -376,17 +389,12 @@ void JSDebuggerAgent::messageReceived(const QByteArray& message)
 
         QList<JSAgentWatchData> locals = getLocals(ctx);
 
-        // Clear any exceptions occurred during locals evaluation.
-        engine()->clearExceptions();
-
         QByteArray reply;
         QDataStream rs(&reply, QIODevice::WriteOnly);
         rs << QByteArray("LOCALS") << frameId << locals;
         sendMessage(reply);
-        state = oldState;
     } else if (command == "SET_PROPERTY") {
-        State oldState = state;
-        state = Stopped;
+        SetupExecEnv execEnv(this);
 
         QByteArray id;
         qint64 objectId;
@@ -402,11 +410,8 @@ void JSDebuggerAgent::messageReceived(const QByteArray& message)
                 QScriptValue result = engine()->evaluate(value);
                 object.setProperty(property, result);
             }
-
-            // Clear any exceptions occurred during locals evaluation.
-            engine()->clearExceptions();
         }
-        state = oldState;
+
         //TODO: feedback
     } else if (command == "PING") {
         int ping;
diff --git a/src/tools/qml/qmlobserver/jsdebuggeragent.h b/src/tools/qml/qmlobserver/jsdebuggeragent.h
index ee8cd049cc0a425ae870dcf2cb65bb561fa4f95a..93d4b75095c0d51004ca22adf2821601fb6a6f94 100644
--- a/src/tools/qml/qmlobserver/jsdebuggeragent.h
+++ b/src/tools/qml/qmlobserver/jsdebuggeragent.h
@@ -103,6 +103,8 @@ public slots:
 //    void pauses();
 
 private:
+    class SetupExecEnv;
+    friend class SetupExecEnv;
 
     enum State {
         NoState,