title: Try/catch with jasmine
body: <p>I have a function which tries to parse a parameter as a JSON object. If it failed, it uses a fallback instead.</p>
  
  <p><strong>parse-code.js</strong></p>
  
  <pre><code>function parseCode(code) {
      try {
          usingJSONFallback(code);
      } catch() {
          usingStringFallback(code);
      }
  }
  
  function usingJSONFallback(code) {
      JSON.parse(code);
      //...more code here
  }
  
  function usingStringFallback(code) {
     //... more code here
  }
  </code></pre>
  
  <p><strong>main.js</strong></p>
  
  <pre><code>//Some code...
  parseCode('hello world!');
  </code></pre>
  
  <p>I'm not seeing any issue in this code. However, when I'm trying to add some unit tests (using Jasmine 2.3) for the 'catch' case, Jasmine catches the JSON parsing error by its own and abort the test:</p>
  
  <p>For instance, for a Jasmine test like:</p>
  
  <pre><code>describe('parseCode', function() {
      it('Parses a string', function() {
          var code = 'My code without JSON';
          expect(parseCode(code)).toThrow();
      });
  });
  </code></pre>
  
  <p>or even a test like:</p>
  
  <pre><code>describe('usingJSONFallback', function() {
     it('Throw an error if there is a string', function() {
        var code = 'My code without JSON';
        expect(usingJSONFallback(code)).toThrow();
     });
  });
  </code></pre>
  
  <p>In both cases, the test fails and returns:</p>
  
  <pre><code>SyntaxError: Unable to parse JSON string
  </code></pre>
  
  <p>I read about throwing controlled exceptions using <code>throw Error(...)</code>, but definitively this doesn't fit well for my case. Any suggestions about how to use Jasmine in this case?</p>