Skip Navigation Links
C# Corner Home
Forum Home
Latest 50
Unanswered
Win Prizes
All Time Leaders
Jump to CategoryExpand Jump to Category
Login 
    Welcome Guest!
 Search Forum For :  
X
 Login
Please login to submit a new post, reply and edit exiting posts, see user profiles, and access more features. If you are not a registered member, Register here.
User Id / Email:
Password:  
Forgot Password | Forgot UserName
   Home » C# Language » try catch problem in C#
       
Author Reply
Muteen ul Haq
posted 6 posts
since Aug 30, 2010 
from

try catch problem in C#

  Posted on: 30 Aug 2010       
Hi!
In program almost all lines we perform such steps in which exceptions can occur. So do we need to use try and catch after all statements. If so, then it will make our program tedious. What is the solution of this question?
I am confuse in using try catch concept. Please help.

Thanks
Tanmay Sarkar
posted  194 posts
since  May 28, 2010 
from  India

 Re: try catch problem in C#
  Posted on: 30 Aug 2010        0  
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.
Tanmay Sarkar
Praveen Raveendran
posted  1 posts
since  Aug 27, 2010 
from 

 Re: try catch problem in C#
  Posted on: 31 Aug 2010        1  

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#
  Posted on: 31 Aug 2010        0  

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#
  Posted on: 31 Aug 2010        0  
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
       
Team Foundation Server Hosting
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
Nevron Chart
 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Advertise with us
Current Version: 5.2011.3.12
 © 1999 - 2012  Mindcracker LLC. All Rights Reserved