spence911 Posted April 26, 2014 Share Posted April 26, 2014 (edited) Hi guys! I would really appreciate it if somebody could explain to me why in the code below the last line "document.write( "Back to safety." );" will only execute if the catch block is not commented out. In other words, the script below will output "Finally!". But if the catch block is not commented out, the output is "Caught your error:someFunction is not definedFinally!Back to safety." <head> <title>Title Time! Yeah</title> <script type="text/javascript"> try { // Throw an error so the catch is triggered. nonExistentFunction(); } /*catch( error ){ // Catch the error. document.write( "Caught your error:", error.message ); } */ finally { document.write( "Finally!" ); } // We got past our try/catch. document.write( "Back to safety." ); </script> </head> <body> </body> </html> Edited April 26, 2014 by spence911 Quote Link to comment https://forums.phpfreaks.com/topic/288042-javascript-error-handling-try-catch-finally/ Share on other sites More sharing options...
trq Posted April 26, 2014 Share Posted April 26, 2014 Because finally will always be called no matter what error is thrown, while your catch is only catching an error of type "error". Quote Link to comment https://forums.phpfreaks.com/topic/288042-javascript-error-handling-try-catch-finally/#findComment-1477377 Share on other sites More sharing options...
spence911 Posted April 26, 2014 Author Share Posted April 26, 2014 (edited) Thanks a lot. That I know. But why does removing the catch block affect the very last line: document.write( "Back to safety." ); That last line will only execute if the catch block is not omitted out. Edited April 26, 2014 by spence911 Quote Link to comment https://forums.phpfreaks.com/topic/288042-javascript-error-handling-try-catch-finally/#findComment-1477379 Share on other sites More sharing options...
spence911 Posted April 28, 2014 Author Share Posted April 28, 2014 *Bump Quote Link to comment https://forums.phpfreaks.com/topic/288042-javascript-error-handling-try-catch-finally/#findComment-1477518 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.