• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 415
  • Last Modified:

Need advice on try catch blocks in c#

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
ksd123
Asked:
ksd123
2 Solutions
 
Shaun KlineLead Software EngineerCommented:
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
 
Raheman M. AbdulCommented:
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
 
Kalpesh ChhatralaSoftware ConsultantCommented:
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
 
ksd123Author Commented:
Thank you all.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now