try catch... FINALLY?
by Admiral Potato
HTML
<div id="output">(empty)</div>
<div>
<button id="boom">
Throw dat error!
</button>
</div>
JavaScript
var outputDiv = document.getElementById('output')
var errorButton = document.getElementById('boom')
errorButton.addEventListener('click', function () {
try {
try {
alert(
window.goats.thisPropertyIsNotPresentOnAnUndefinedObject
)
} catch (e) {
outputDiv.innerHTML += '<h2>inner catch:' + e.message + '</h2>'
// re-throw the error because this would normally stop things
throw e
} finally {
outputDiv.innerHTML += '<p>inner `finally`</p>'
}
outputDiv.innerHTML += '<p>no inner error</p>'
} catch (e) {
outputDiv.innerHTML += '<h2>outer catch:' + e.message + '</h2>'
// re-throw the error because this would normally stop things
throw e
} finally {
outputDiv.innerHTML += '<p>outer `finally`</p>'
}
outputDiv.innerHTML += '<p>no outer error</p>'
})