Solved

Need advice on try catch blocks in c#

Posted on 2013-05-22
4
403 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.

734 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