Exception demo
HTML
What to throw:
<select id='tothrow'>
<option>nothing</option>
<option>anonymous object</option>
<option>MyCustomExceptionClass</option>
<option>MyCustomExceptionClass2</option>
</select>
<button id='run'>run</button>
JavaScript
var btnRun = document.getElementById('run');
btnRun.addEventListener("click", run, false);
var selToThrow = document.getElementById('tothrow');
function MyCustomExceptionClass() {};
function MyCustomExceptionClass2() {};
function run()
{
var data = {
bookId: getCurrentBookId(),
chapterNumber: getCurrentChapter(),
currentElementId: $(getUppermostVisibleElement()).attr('id')
};
var data2 = "test";
try
{
switch(selToThrow.value)
{
case 'nothing':
alert(data);
break;
case 'anonymous object':
break;
case 'MyCustomExceptionClass':
throw new MyCustomExceptionClass();
break;
case 'MyCustomExceptionClass2':
throw new MyCustomExceptionClass2();
break;
}
}
catch(e)
{
alert('Something was caught.');
if (e instanceof MyCustomExceptionClass)
{
alert('MyCustomExceptionClass');
}
else if (e instanceof MyCustomExceptionClass2)
{
alert('MyCustomExceptionClass2');
}
else
{
alert('anonymous object with value "' + e.value + '"');
}
}
finally
{
alert('The finally block always runs.');
}
}