Hello Developers, this is not a detailed article. Instead, this is just a list of few tricky questions on exception handling which you might have faced in so many interviews.
Can we use multiple finally blocks inside the exception handling c# method?
Yes, we can write the multiple finally blocks in exception handling c#. For every try block, there needs to be either a catch block or finally block.
try { try { } catch() // more than one catch blocks allowed { } finally // allowed here too. { } } catch() { } finally { }
Is it possible to keep finally inside a try block?
Yes, it is possible to keep finally inside a try block. An example of the same is shown above.
Is it possible to keep multiple catch blocks for a try block?
Yes, it is possible to have multiple catch blocks associated with a try block. However, only one catch block can ever handle an exception at a time. Also, we need to make sure the generic exception block is always at the end.
try { } catch(ArgumentNullException ex) { } catch(Exception ex) { } finally { }
Can we write try block without catch block?
No, It’s not allowed to write a try block without a catch/finally block. You have to write one of either catch or finally with each try block. You will be prompt with a compilation error.
Can we write try block only with finally block in exception handling c#?
Yes, you can write a try block only with the finally block.
try { } finally { }