views:

27

answers:

2

How to catch exceptions raised by a referenced javascript file in an aspx page??

+3  A: 

You can't catch an exception that occurs on the user's browser from the web server. This is not due to some ASP.NET limitation, it's just that the code (javascript) that executes on the client has nothing to do with the code that executes on the server, probably thousands of miles away. If you want to catch and handle a javascript exception you should use javascript's try...catch mechanism

What are you trying to do? Are you trying to trace bugs in your code? Do you want to be notified if a client's browser can't handle your javascript? Or do you want to notify the server that something went wrong in the client?

EDIT What you are really looking for is a logging framework for javascript. There are multiple logging frameworks out there, some of which support logging to a remote server through Ajax, e.g. log4javascript and log4js .

Panagiotis Kanavos
Actually, I want to catch the exception at client side and store it in server using Ajax for future improvements.
Mahesh
What you are looking for is logging for Javascript.
Panagiotis Kanavos
Depends on what you wanted to log, you can use framework or DIY
airmanx86
When the question is "how can I do it", the DIY answer is not the best. Besides, there is always DRY
Panagiotis Kanavos
A: 

Maybe you want to this.

<head runat="server">
    <title></title>
    <script type="text/javascript" src="JScript.js"></script>
</head>
<body>
  <form id="form1" runat="server">
      <button type="button" onclick="try{ testException();} catch(e){alert(e);}">Test Exception</button>
   </form>
</body>

JScript.js

function testException() {
    e.data = 10;
}
adatapost