How to catch exceptions in javascript?

后端 未结 4 529
栀梦
栀梦 2020-12-10 03:44

I want to catch exceptions in javascript if an insertion query is not done.

I have written the code below:

var adoConn = new ActiveXObject(\"ADODB.Co         


        
相关标签:
4条回答
  • 2020-12-10 04:01
    try {
      // your code that can throw exception goes here
    } catch(e) {
      //do stuff with the exception
    }
    

    FYI - the code you posted looks, well, for want of a better word, ugly! (No offense) Couldn't you use DWR or some other JavaScript framework (depending on your language choice) to hide all the DB connection stuff at the back end and just have the javascript calling the back end code and doing something with the response?

    0 讨论(0)
  • 2020-12-10 04:05
    try {
        adoConn.Execute("insert into session (SessionId,Timestamp) values ('"
                         + SessionId + "','" 
                         + SessionCurrenttime + "')");
    } catch(e) {
        /*use error object to inspect the error: e.g. return e.message */
    }
    
    0 讨论(0)
  • 2020-12-10 04:14
    <script language="JavaScript">
    
    try
    {
     colours[2] = "red";
    }
    catch (e)
    {
      alert("Oops! Something bad just happened. Calling 911...");
    }
    
    </script> 
    

    (Ripped from http://www.devshed.com/c/a/JavaScript/JavaScript-Exception-Handling/)

    0 讨论(0)
  • 2020-12-10 04:16

    To be complete, here's the full structure

    try {
      // your code that can throw exception goes here
    } catch(e) {
      //do stuff with the exception
    } finally {
      //regardless if it worked or not, do stuff here (cleanup?)
    }
    
    0 讨论(0)
提交回复
热议问题