title: HTTP POST error Handling in Swift 2
body: <p>I'm new here, and this is my first question...
  I try to write an App in Swift 2 that makes an HTTP POST request but I can't figure out how to use the new error handling of Swift 2. Can anyone tell me please how to implement the "<em>do-try-catch</em>" error handling of Swift 2 to the code snippet below?
  (This code snippet uses the old error handling of swift 1.2)</p>
  
  <pre><code>func post(params : Dictionary&lt;String, String&gt;, url : String) {
      var request = NSMutableURLRequest(URL: NSURL(string: url)!)
      var session = NSURLSession.sharedSession()
      request.HTTPMethod = "POST"
  
      var err: NSError?
      request.HTTPBody = NSJSONSerialization.dataWithJSONObject(params, options: nil/*, error: &amp;err*/)
      request.addValue("application/json", forHTTPHeaderField: "Content-Type")
      request.addValue("application/json", forHTTPHeaderField: "Accept")
  
      var task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -&gt; Void in
          print("Response: \(response)")
          var strData = NSString(data: data!, encoding: NSUTF8StringEncoding)
          print("Body: \(strData)")
          var err: NSError?
          var json = NSJSONSerialization.JSONObjectWithData(data!, options: .MutableLeaves/*, error: &amp;err*/) as? NSDictionary
  
          // Did the JSONObjectWithData constructor return an error? If so, log the error to the console
          if(err != nil) {
              print(err!.localizedDescription)
              let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding)
              print("Error could not parse JSON: '\(jsonStr)'")
          }
          else {
              // The JSONObjectWithData constructor didn't return an error. But, we should still
              // check and make sure that json has a value using optional binding.
              if let parseJSON = json {
                  // Okay, the parsedJSON is here, let's get the value for 'success' out of it
                  var success = parseJSON["success"] as? Int
                  print("Succes: \(success)")
              }
              else {
                  // Woa, okay the json object was nil, something went worng. Maybe the server isn't running?
                  let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding)
                  print("Error could not parse JSON: \(jsonStr)")
              }
          }
      })
  
      task!.resume()
  }
  </code></pre>