title: How to catch a .NET exception in javascript?
body: <p>I'm building a webapp using ASP C#.NET MVC3.</p>
  
  <p>I have a View (<strong>.cshtml file</strong>). In this View I have a javascript function "SomeFunction()" which calls a .NET function through <strong>Razor</strong> like so: "@Html.Raw(Json.Encode(Model))". So everything put together it looks like:</p>
  
  <pre><code>SomeFunction(){
      var sections = @Html.Raw(Json.Encode(Model));
  }
  </code></pre>
  
  <p>Note the @ please.</p>
  
  <p>This function throws an exception (this exception to be exact: <a href="https://stackoverflow.com/questions/15288367/increase-json-response-maxjsonlength-in-mvc-4">Increase json response maxJsonLength in MVC 4</a>) when the size of the string exceeds the maximum (too many sections basically). 
  Well the requirements of my website don't instruct me to actually deal with this (luckily) by retrieving the data in pieces async and whatnot. 
  I'm fortunate enough to simply catch this exception and tell the user: "Too many sections! Please define a larger size for sections." Or something like this.
  Alas I don't really understand how to do this exactly. I've tried the following:</p>
  
  <pre><code>SomeFunction(){
      try {
          var sections = @{try {
              Html.Raw(Json.Encode(Model));
          } catch (Exception e) {
              throw new Exception("Exception caught in .NET");
          }}
      } catch(err) {
          alert('.NET exception caught in Javascript!');
      }
  }
  </code></pre>
  
  <p>Note the @ infront of the 'try' to denote Razor syntax.</p>
  
  <p>The 'alert' in the catch clause in javascript is never reached. I come from desktop dev so I'm still a web novice. How is this normally done? </p>
  
  <p>Thanks a lot in advance for helping me.</p>