1

I need your help in implementing an exception code with 'try ~ catch' in C#.

My motive is to follow 'try ~ except' in python like below:

try: 
    pass 

except KeyboardInterrupt as e: 
    print("Closed...") 
    break 

I thought there was also the interrupt exception keyword like Python in C#.
So, I tried like:

try 
{
    // pass 
}
catch (ThreadInterruptedException e) 
{
    Console.Write("Closed...");
    break;
}

But, this C# code couldn't catch the interrupt signal by ctrl+C.
I assumed 'ThreadInterruptedException' is for that, but it isn't.

Which keyword should I use?

I have tried to google, but there are a number of examples about 'how to get 'ctrl+v' value through my console.

윤건우
  • 59
  • 5
  • 2
    Have you seen [how-do-i-trap-ctrl-c-sigint-in-a-c-sharp-console-app](https://stackoverflow.com/questions/177856/how-do-i-trap-ctrl-c-sigint-in-a-c-sharp-console-app) ? – Caius Jard Feb 27 '20 at 07:25

1 Answers1

1

You can do this with Console.CancelKeyPress.

https://learn.microsoft.com/de-de/dotnet/api/system.console.cancelkeypress?view=netframework-4.8

Max
  • 21
  • 5