What is a catching in C#? [Resolved]

Posted by Amatya under C# on 3/17/2016 | Points: 10 | Views : 248 | Status : [Member] | Replies : 2
What is a catching in C#? when and where to use with a suitable example.

Feel free to share informations 9731764134
mail Id ' adityagupta200@gmail.com
Thanks



Responses

Posted by: Rajnilari2015 on: 3/17/2016 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
@Amatya sir,
While writing a piece of code, there might be some logical errors that we may not predict. In that case if we run the program though it will compile and work in the best fit, but will fail in the exception scenarios. E.g.

int x = 50;
int y = 10;
int z = x/y;

the output is 5. This is the best case scenario.

But what about y=0;

You will encounter Divide by zero exception.

This scenarios cannot be caught until runtime.
Henceforth, as a best practice, we use Try..Catch block. e.g.

try {

int x = 50;
int y = 0;

int z = x/y;
}catch(DivideByZeroException ex) {Console.WriteLine(ex.message); } //for specific exception
catch(Exception e) { Console.Writeline(e.message);} //for any other kind of exceptions.


You can also refer to this ( http://www.dotnetfunda.com/articles/show/3185/generate-pdf-and-send-as-attachment-in-email-using-itextsharp-and-exce ) article of mine for more understanding so using the same.

Let us know if that helps.

--
Thanks & Regards,
RNA Team

Amatya, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Amatya on: 3/17/2016 [Member] Silver | Points: 25

Up
0
Down
Oh. Is it ..sorry brother. I thought that catching is related to storing the information in RAM or some where in memory.

Sorry for taking your time.

Thanks


Feel free to share informations 9731764134
mail Id ' adityagupta200@gmail.com
Thanks

Amatya, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response