title: Lua Catching Exceptions
body: <p>I am coding in Lua (and C++).
  I want to catch exceptions and printing them into console. After <code>lua_atpanic</code> did not work correctly (program exited anyway). I thought to use exceptions.</p>
  
  <p>Here is the edited part of my <code>luaconf.h</code>:</p>
  
  <pre><code>/* C++ exceptions */
  #define LUAI_THROW(L,c) throw(c)
  #define LUAI_TRY(L,c,a) try { a } catch(...) \
  { if ((c)-&gt;status == 0) (c)-&gt;status = -1; }
  #define luai_jmpbuf int  /* dummy variable */
  </code></pre>
  
  <p>Here is the <code>init.lua</code> loaded:</p>
  
  <pre><code>int init = luaL_loadfile(L, "lua/init.lua");
  if(init == 0)
  {
      printf("[LUA] Included lua/init.lua\n");
      init = lua_pcall(L, 0, LUA_MULTRET, 0);
  }
  </code></pre>
  
  <p>So now I thought, when using C++ exceptions I would edit that code to the following:</p>
  
  <pre><code>try {
      int init = luaL_loadfile(L, "lua/init.lua");
  
      if(init == 0)
      {
          printf("[LUA] Included lua/init.lua\n");
          init = lua_pcall(L, 0, LUA_MULTRET, 0);
      }
  
      // Error Reporting
      if(init != 0) {
          printf("[LUA] Exception:\n");
          printf(lua_tostring(L, -1));
          printf("\n\n");
          lua_pop(L, 1);
      } else {
          lua_getglobal(L, "Init");
          lua_call(L, 0, 0);
      }
  } catch(...)
  {
      MessageBox(NULL, "Hi", "Hio", NULL);
  }
  </code></pre>
  
  <p>Just to see if anything happens. But nothing happens.
  (The Lua error is calling a <code>nil</code> value)</p>
  
  <p>Any ideas?</p>