Question: I was trying to test whether certain file exist using javascript.(don't want to use Ajax). Want to use catch try to handle ERR\_FILE\_NOT\_FOUND error. I tried 

```
   try{
            img.attr('src', url)
        }catch(err){
            console.log("file"+ url + " doesn't exist");
        }

```

but it doesn't catch up the error.How do I catch ERR\_FILE\_NOT\_FOUND error?

Answer: 
Chrome does it to inform. Even if you handle `onerror` correctly with correct error handling with `try-catch` and every trick with `void` or `( )` that is told to prevent error - **you can not fix it**. It is out of Javascript control. 

The best you can do is to let those errors scroll away to the top, to not clutter your output. This works if you also want to remove the clutter of line numbers on each `console.log` to get a clean output with relevant information. Use `\n` as line break. A dashed line separate the errors from your output. 

```
var logs = ""

function log(x) {
  logs += '\n\n' + x
}

//your code
try{
  img.attr('src', url)
}catch(err){
  log("file"+ url + " doesn't exist")
}

setTimeout(function(){
  console.log("%c" + logs, "border-top: dashed 2px black;")
}, 1000)

```

Maybe better replace the `setTimeout` with a function called in end of your program.
The only way I found is making a fake request to check the file is wherever exist or not like this:

```
  var request;
  if(window.XMLHttpRequest)
      request = new XMLHttpRequest();
  else
  request = new ActiveXObject("Microsoft.XMLHTTP");
  request.open('GET', url, false);
  request.send();
  // the object request will be actually modified
  if (request.status === 404) {
      console.log("file"+ url + " doesn't exist");
  } 
  else
  {
      img.attr('src', url)
  }

```

Notice: It only works with same origin, so if you need to check the file in other origins. you need to use Ajax or backend languages.