Solved

Need advice on try catch blocks in c#

Posted on 2013-05-22
4
404 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 26

Accepted Solution

by:
Shaun Kline earned 100 total points
ID: 39187277
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 19

Assisted Solution

by:Raheman M. Abdul
Raheman M. Abdul earned 50 total points
ID: 39187284
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
ID: 39187564
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
ID: 39189731
Thank you all.
0

Featured Post

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

717 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