pages

Showing posts with label Debugging. Show all posts
Showing posts with label Debugging. Show all posts

Wednesday, 7 April 2021

Catching Exceptions with "when"

TLDR;
Use when with a catch block to get hold of exceptions without unwinding the stack.
You can use it to handle exception based on specific property, condition or context.


we were catching exceptions just fine before the introduction of when keyword. I must say, its a good feature released in C# 6.


catch (ExceptionType e) when (expression) 
expression should return a boolean value, if the result is true the ExceptionHandler executes. The only difference I found, it gives you provision to check the variables value in a debug session. I dont see any production value in this feature.


Exception filters (when):
The filter expression is evaluated before the stack is unwound. This means the original call stack and all local variables remain intact during filter evaluation.

  • Handling exception using specific property

    catch (IOException ex) when (ex.Message.Contains("access denied"))
    {                                                                 
        Console.WriteLine("File access denied. Check permissions.");  
        Console.WriteLine(ex.StackTrace);                             
    }                                                                 
    catch (IOException ex) when (ex.Message.Contains("not found")) { Console.WriteLine("File not found. Verify the path."); Console.WriteLine(ex.StackTrace); }
    catch (Exception ex) { Console.WriteLine("general catch all"); Console.WriteLine(ex.StackTrace); }

You wont get to see any difference in the above 3 stack traces printed in the exceptions.

It helps us in examining exception before deciding to handle them and lets us handle based on the context.


catch (IOException ex) when (filePath.contains(@"c:\users"))          
{                                                                     
    Console.WriteLine($"Network file operation failed: {ex.Message}");
}                                                                     


Why do we need multiple catch blocks? why not just use a catchall with (Exception)
  • Catching specific exceptions can help you take more granular decision based on the situation.
  • It can tell you a lot more about the error occurred using the properties specific to that Exception type. Yes they all are derived from Exception but every class has some specific properties limited to them and you can access them if you catch them. Exception handling is a feature that helps the developer to avoid getting embarrassed in front of the client. But using when clause in exception handling is a feature that helps the developer with getting to know about the error better.

Wednesday, 30 October 2019

Debugging Azure functions locally

Actually the title of this post should be "Debugging your serverless apps locally".
I found a utility while working on Azure functions which helps you debug the function locally(if you have the source code, obviously). It is called ngrok. They have a pretty awesome website and very simple docs.


Steps to proceed are as below.
  1. Make sure your Azure function is up and running in your machine and you are able to hit the breakpoint from your local.
  2. Signup on the ngrok website.
  3. Download the ngrok utility(its an exe file). double click to execute. It opens a command prompt.
  4. Connect to your account. Upon login to ngrok website you will get an auth token which you have to use to connect to your account. command to execute "ngrok authtoken <your_token_here>". This step will create a .yml file in your users directory.
  5. Create a tunnel. Use command "ngrok http 7071". 7071 is the port you want to expose. I used it because its the default port for Azure function apps.
  6. Use the url provided by the ngrok utility to hit the breakpoint on your function from the internet.
I hope this was easy. Please share your thoughts in the comments below.