try-catch-finally-nested.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. test("Nested try/catch/finally with exceptions", () => {
  2. // This test uses a combination of boolean "checkpoint" flags
  3. // and expect().fail() to ensure certain code paths have been
  4. // reached and others haven't.
  5. var level1TryHasBeenExecuted = false;
  6. var level1CatchHasBeenExecuted = false;
  7. var level1FinallyHasBeenExecuted = false;
  8. var level2TryHasBeenExecuted = false;
  9. var level2CatchHasBeenExecuted = false;
  10. var level3TryHasBeenExecuted = false;
  11. var level3CatchHasBeenExecuted = false;
  12. var level3FinallyHasBeenExecuted = false;
  13. expect(() => {
  14. try {
  15. level1TryHasBeenExecuted = true;
  16. foo();
  17. expect().fail();
  18. } catch (e) {
  19. level1CatchHasBeenExecuted = true;
  20. try {
  21. level2TryHasBeenExecuted = true;
  22. try {
  23. level3TryHasBeenExecuted = true;
  24. bar();
  25. expect().fail();
  26. } catch (e) {
  27. level3CatchHasBeenExecuted = true;
  28. } finally {
  29. level3FinallyHasBeenExecuted = true;
  30. baz();
  31. expect().fail();
  32. }
  33. expect().fail();
  34. } catch (e) {
  35. level2CatchHasBeenExecuted = true;
  36. qux();
  37. expect().fail();
  38. }
  39. expect().fail();
  40. } finally {
  41. level1FinallyHasBeenExecuted = true;
  42. throw Error("Error in final finally");
  43. expect().fail();
  44. }
  45. expect().fail();
  46. }).toThrow(Error, "Error in final finally");
  47. expect(level1TryHasBeenExecuted).toBeTrue();
  48. expect(level1CatchHasBeenExecuted).toBeTrue();
  49. expect(level1FinallyHasBeenExecuted).toBeTrue();
  50. expect(level2TryHasBeenExecuted).toBeTrue();
  51. expect(level2CatchHasBeenExecuted).toBeTrue();
  52. expect(level3TryHasBeenExecuted).toBeTrue();
  53. expect(level3CatchHasBeenExecuted).toBeTrue();
  54. expect(level3FinallyHasBeenExecuted).toBeTrue();
  55. });