Azure functions - How to handle exceptions from output binding? RRS feed

  • Question

  • Say I have a function that is triggered by event hub and has an output binding to document db, and I would like to catch any exception thrown by document db and send the event to a queue when that happens, is that possible? In general, is there a way to handle exceptions thrown from the output binding in Azure functions? Or should I use something else like Webjob for this kind of logic? 
    • Edited by cliffyq Tuesday, May 16, 2017 7:03 AM
    Tuesday, May 16, 2017 7:00 AM

All replies

  • Both Azure Functions and core WebJobs are going to be the same in this regard. Your options for handling exceptions will vary depending on language you're using, and the Type you're binding to. For example, if you're using C#, you can bind to an IAsyncCollector and try catch around the call to AddAsync as below.

    #r "Newtonsoft.Json"
    using System;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Linq;
    public static async Task Run(string input, IAsyncCollector<JObject> docs, TraceWriter log)
        log.Info($"C# manually triggered function called with input: {input}");
        var doc = new JObject()
            { "name", "Ruby" }
            await docs.AddAsync(doc); 
            // handle exception

    Mathew Charles [MSFT]

    Tuesday, May 16, 2017 5:18 PM