How to log exceptions in JavaScript

后端 未结 7 1514
太阳男子
太阳男子 2020-12-15 03:52

As a C# developer I\'m used to the following style of exception handling:

try
{
    throw SomeException(\"hahahaha!\");
}
catch (Exception ex)
{
    Log(ex.T         


        
7条回答
  •  甜味超标
    2020-12-15 04:29

    You can use almost in the same manner ie.

    try
    {
        throw new Error("hahahaha!");
    }
    catch (e)
    {
        alert(e.message)
    }
    

    But if you want to get line number and filename where error is thrown i suppose there is no crossbrowser solution. Message and name are the only standart properties of Error object. In mozilla you have also lineNumber and fileName properties.

提交回复
热议问题