JS Error Handling


try ... catch ... finally can handle errors in javascript.

For example, if we call an undefined function, an error will occur:

try
{
	nafunc();
}
	catch (e)
{
	alert(e.description); //will popup "undefined"
}


The finnaly{...} part is optional, it will be executed regardless of error.

try
{
	nafunc();
}
catch (e)
{
	alert(e.description);
}
finally
{
	alert("over");
}


The throw() function customizes the raised exception.

var ay = new Array(1,0,4);
for (var i=1;i<3;i++)
{
	try
	{
		if (ay[i] == 0) {throw("divided by zero");}
	}
	catch (e)
	{
		alert(e); //will popup "divided by zero"
	}
}


convert.idontcarewhatyouthink.net © 2021  Terms of Use | Home