Tanmay Sarkar
posted
194 posts
since
May 28, 2010
from
India
|
|
Re: try catch problem in C#
|
|
|
|
|
|
|
|
|
|
|
Suppose your program take a integer input from user, & you return that with multiplied by 2,
the exception cases are, user's input is not right form so there an exception occurred.
so we use try-catch to avoid it.
for more details please see the recommended articles section.
Thank you!
If it help you then mark it as an answer.
|
|
|
|
|
|
Praveen Raveendran
posted
1 posts
since
Aug 27, 2010
from
|
|
Re: try catch problem in C#
|
|
|
|
|
|
|
|
|
|
|
Hi Muteen,
No need to use try catch in your entire program. Try to find out the module where there is a chance of getting any error.
For example if a module is doing division action. There is a possibility of 0/0 error as well as dividebyzero error (eg: 5/0).
In these circumstances we need to use try catch statement. It would be better to catch the ArithmeticException as well as the General Exception.
I hope you understood.
|
|
|
|
|
|
Pasan Ratnayake
posted
58 posts
since
Apr 06, 2009
from
Sri Lanka
|
|
Re: try catch problem in C#
|
|
|
|
|
|
|
|
|
|
|
It is actually of no use for us to try and propose solutions without knowing the true nature of your problem. But if you could group the actions that might throw exceptions, create methods where such actions are performed through the methods.
For instance, if you get a divisable by zero exception everywhere, you could write a simple method that divides 2 numbers where you place the try-catch block. This way you can still perform the task everywhere in you application encapsulated in a try-catch block which reduces the repeated try-catch blocks. Of course there are occasions where this is not applicable but it could help.
|
|
|
|
|
|
Shankey
posted
179 posts
since
Aug 05, 2010
from
|
|
Re: try catch problem in C#
|
|
|
|
|
|
|
|
|
|
Hi muteen, I agree with Praveen Raveendran reply and further added it will protect your application from getting crashed. So that the user are not having wrong impression of your application. Try...catch clause allows your application to run smoothly even if there are chances of exception to occur like the same one if you want to perform division and you are taking it from user then there are chances of division by zero error. so to avoid that you have to write only your "Division code" in TRY....CATCH clause.
If it help you then mark it as an accepted.
|
|
|
|
|
If this post helped you, then tick the checkbox above "Do you like this Answer"
Shakey Shankey
|
|
|
|
|
|