Javascript 如何使用XMLHttpRequest捕获无效URL引发的错误

Javascript 如何使用XMLHttpRequest捕获无效URL引发的错误,javascript,ajax,html,xmlhttprequest,Javascript,Ajax,Html,Xmlhttprequest,我对JavaScript中的XMLHttpRequest对象没有太多的使用。我是新手。我已经完成了许多jQueryAjax调用,但没有对XMLHttpRequest对象进行过太多详细的处理 我正在阅读一本书,书中他们给出了一些使用这个对象的示例代码 下面是本书在处理错误时使用的一些代码。它应该捕获错误URL引发的错误。但是我无法让它继续执行catch子句: var httpRequest = new XMLHttpRequest(); try { httpRequest.open("G

我对
JavaScript
中的
XMLHttpRequest
对象没有太多的使用。我是新手。我已经完成了许多
jQuery
Ajax
调用,但没有对
XMLHttpRequest
对象进行过太多详细的处理

我正在阅读一本书,书中他们给出了一些使用这个对象的示例代码

下面是本书在处理错误时使用的一些代码。它应该捕获错误URL引发的错误。但是我无法让它继续执行
catch子句

var httpRequest = new XMLHttpRequest();
try
{
     httpRequest.open("GET", "http://");
     httpRequest.send();
}
catch (error)
{
     alert("in catch clause");
}
httpRequest.open("GET", "gttp://");
我甚至尝试了一个无效的URL,但仍然不想进入
catch子句

var httpRequest = new XMLHttpRequest();
try
{
     httpRequest.open("GET", "http://");
     httpRequest.send();
}
catch (error)
{
     alert("in catch clause");
}
httpRequest.open("GET", "gttp://");
是因为URL是有效的URL,所以它没有引发异常吗?

var-httpRequest=new-XMLHttpRequest();
尝试
{
open(“GET”,“http://”,false);
httpRequest.send();
}
捕获(错误)
{	 
警告(“捕获条款”);

}
传递未定义变量时捕获的错误
httpRequest.open(“GET”,未定义)我尝试了
httpRequest.open(“GET”,false)它调用,null和undefined也是如此。