codegurukool.com · Free coding tutorials & online compiler

Home › JavaScript Tutorial › JS errors

JS errors

Handle problems safely.

try runs risky code, catch handles the error, finally always runs and throw raises your own error.

Example code

<pre id='o'></pre>
<script>
let t='';
try{null.x;}catch(e){t+=e.name+': '+e.message+'\n';}
try{throw new Error('Custom');}catch(e){t+=e.message+'\n';}finally{t+='finally ran';}
o.textContent=t;
</script>

Run this code in the online editor

← JS data typesJS debugging →