try1.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. // basic try/catch testcases
  6. function verify(x,y)
  7. {
  8. if(x != y)
  9. WScript.Echo("ERROR: " + x + " != " + y);
  10. }
  11. var objs = [5, undefined, 'c', "test", [1,2,3] ];
  12. for(var i = 0; i < objs.length; ++i)
  13. {
  14. // test #1: basic try/catch
  15. try
  16. {
  17. throw objs[i];
  18. }
  19. catch(a)
  20. {
  21. WScript.Echo("caught " + a);
  22. verify(a, objs[i]);
  23. }
  24. // test #2: try/finally within a try/catch/finally
  25. try
  26. {
  27. try
  28. {
  29. throw objs[i];
  30. }
  31. finally
  32. {
  33. WScript.Echo("inner finally, i = " + i);
  34. }
  35. }
  36. catch(a)
  37. {
  38. WScript.Echo("caught " + a);
  39. verify(a, objs[i]);
  40. }
  41. finally
  42. {
  43. WScript.Echo("outer finally, i = " + i);
  44. }
  45. // test #3: more deeply nested try/catch/finally
  46. try
  47. {
  48. try
  49. {
  50. try
  51. {
  52. throw objs[i];
  53. }
  54. finally
  55. {
  56. WScript.Echo("finally #3, i = " + i);
  57. }
  58. }
  59. catch(a)
  60. {
  61. WScript.Echo("caught " + a);
  62. verify(a, objs[i]);
  63. }
  64. finally
  65. {
  66. WScript.Echo("finally #2, i = " + i);
  67. throw "another throw";
  68. }
  69. }
  70. catch(a)
  71. {
  72. WScript.Echo("caught " + a);
  73. verify(a, "another throw");
  74. }
  75. }