Try and catch block Interview questions


Try and catch block


Difference btw throw end throw ex.

I am writing this post someone asked to me and I never heard earlier before.
I thought both are same, and one of them give an error, but both are wrong,
Now I have try to give you a proper answer


Similarities:
Let’s first see what is similar in both.

  • Both are used to throw exception in catch block to log message
  • Both contains same message of exception

Difference:
Now let’s see what is difference.

·         throw is used to throw current exception while throw(ex) mostly used to create a wrapper of exception.
  • throw(ex) will reset your stack trace
                            I.        so error will appear from the line where throw(ex) written
                         II.        while throw does not reset stack trace and you will get information about original exception.
  • In MSIL code when you use throw(ex) it will generate code as throw and if you use throw it will create re throw.





Notes: throw will get information about original exception point line no 13 as well as line where throw ex written







Can we have multiple try catch block

Yes, we can write multiple try catch block, but
·         if you write main catch (Exception ex) at the top the in .net other you cannot write it’s show a compile time error to you
·         Every try has at least one catch block



Can we have multiple try block

Yes, we can write multiple try block, but
·         Every try has at least one catch block



Can we skip catch block after try.

No , see above question it will help you  

Can we have same exception class

No, see the below image it will help you 



What happen if we have multiple catch blocks in a class




What happen if we write a finally block after catch blocks in a class









Download PDF
Share:

No comments:

Post a Comment

Popular

Tags

Mobile

Recent Posts