Solved

Need advice on try catch blocks in c#

Posted on 2013-05-22
4
391 Views
Last Modified: 2013-05-22
Hi Experts,

I have web application with 8 buttons  and each button has different functionality and am  using try {} catch {} blocks in each button click event.

1)Can I use  try,catch blocks  this many times in a page ? Is this best practice?

2)Is there any differnce between below 2 statements?

a)Class1 obj;
obj=new Class1();

b)Class2 obj=new Class2();

Thanks in advance
0
Comment
Question by:ksd123
4 Comments
 
LVL 25

Accepted Solution

by:
Shaun Kline earned 100 total points
Comment Utility
You can use as many Try/Catch/Finally blocks as you need, including having nested levels. A best practice is to use Try/Catch blocks where ever you need to process an known exception (such as trying to write to a file that is open by another process).

Technically, there is no difference between your two statements, in that after the last line of each example you will have a created and instantiated object. However, the first example provides more control, in that you can delay the instantiation until you need it.
0
 
LVL 18

Assisted Solution

by:Raheman M. Abdul
Raheman M. Abdul earned 50 total points
Comment Utility
1. If the exception you are catching are different then you can use as many times but in my view this does not effect.

2. There is no difference.
0
 
LVL 16

Expert Comment

by:Kalpesh Chhatrala
Comment Utility
1. you can use Try Catch as per your requirement. if possible write exception in some file or database. so you can trace it easily.
 
try this tool for Error logging ; https://code.google.com/p/elmah/

2. No difference.
0
 

Author Closing Comment

by:ksd123
Comment Utility
Thank you all.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now