I am trying to get the ideal way to handle exception. I googled & read that I should put try
catch
in the catch
block as well to handle but what if any exception occurs in the nested block itself.
try
{
int a = 10;
int b = 0;
int c = a / b;
Console.WriteLine(c);
Console.ReadKey();
}
catch (Exception ex)
{
int a = 10; int b = 0;
int c = a / b;
Console.WriteLine(ex.Message.ToString());
Console.ReadKey();
}
finally
{
Console.WriteLine("Some Exception");
}
On googling I read that it should be decorated as below:
- If exception occurs in Catch block itself then how to handle it in C#?
- If exception occurs in Catch block itself then how to handle it in C#?
-
try { int a = 10; int b = 0; int c = a / b; Console.WriteLine(c); Console.ReadKey(); } catch (Exception ex) { try { } catch(Exception innerEx) { // What if exception here also occurs. } } finally { Console.WriteLine("Some Exception"); }
If I do this way, then it will stuck in an infinite
try-catch
block.
I think there would be some better or the right way to handle this scenario.