8

Simplification — I've created an empty AWS Lambda project with .net CORE :

enter image description here

This is the default empty lambda function project :

enter image description here

I want to catch all exception in the app , globally.

So I've created a method that generates an exception, and added a global application handler :

Complete code :

 public class Function
    {
        void CreateException()
        {
            var z = 0;
            var a = 2 / z;
            Console.Write(a);
        }

        public Function()
        {
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
        }

        public void FunctionHandler(object input, ILambdaContext context)
        {
            CreateException();
        }

        private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            // � It never gets here
        }

    }

The problem is that the exception is raised , but it never fires CurrentDomain_UnhandledException

Question:

Is this the right way of catching global exceptions ? and why doesn't CurrentDomain_UnhandledException invoked when there is an unhandled exception ?

1

1 Answer 1

3
+50

While I cannot answer why this isn't working, I can tell you I have done as a workaround.

The workaround I have used is to wrap my FunctionHandler in a try/catch. Since this is the only entrypoint in the lambda function, anything that gets thrown in here will be caught...effectively acting like a global handler for my purposes.

public void FunctionHandler(object input, ILambdaContext context)
{
    try
    {
        CreateException();
    }
    catch (Exception e)
    {
        // Handle your 'uncaught' exceptions here and/or rethrow if needed
        Console.WriteLine(e);
        throw;
     }
}

You would likely want to rethrow so that:

  1. AWS will retry the function again, then you can send it DeadLetterQueue after X retries
  2. You get the exception in your CloudWatch Logs
Sign up to request clarification or add additional context in comments.

5 Comments

If a function fails , it re-runs it again ?? where is that setting?
It depends on how your Lambda function is getting called. Definitely give this a read - docs.aws.amazon.com/lambda/latest/dg/retries-on-errors.html I've found you have to be very careful with things that are stream-based. Something like Dynamo will keep retrying until it doesn't throw an exception or the stream expires. If you aren't prepared for this, you can definitely build up some costs...I found that out the hard way when I had a process failing all weekend.
I just tried exactly this and came to the same conclusion... however I have dozens of Lambda functions in my project, so this is a real pain.
This is NOT a global exception handler as it wouldn't catch a single constructor error!
This does not provide the solution requested - it is not global.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.