comments | Javascript source | Execution result in your current browser |
---|---|---|
display run-time error | try { var a=b+1; document.write("b="+b); } catch (e) { document.write("<font color=red>"+e.message+"</font>"); } finally { document.write("<p/> show me always"); } |
|
variables scope, a variable defined inside a try block can be accessed in a following try block | try { var val=120; } catch (e) { document.write("<font color=red>"+e.message+"</font>"); } finally { document.write("<p/> was trying to define variable val"); } try { document.write("<p/> in second try block, val="+val); } finally { document.write("<p/> was trying to show variable val"); } |
|
throw a user defined error | function expectPositive(a) { if (a<0) { var error=new Object(); error["message"]="argument has to be positive"; throw error; } return a; } try { var b=expectPositive(-2); } catch (e) { document.write(e.message); } |