Click here to Skip to main content
15,916,842 members
Home / Discussions / C#
   

C#

 
GeneralRe: Components: create runtime/designtime bindable property, possible? Pin
Wendell.S23-Nov-09 23:52
Wendell.S23-Nov-09 23:52 
GeneralRe: Components: create runtime/designtime bindable property, possible? Pin
dojohansen24-Nov-09 0:14
dojohansen24-Nov-09 0:14 
GeneralRe: Components: create runtime/designtime bindable property, possible? Pin
Gerry Schmitz24-Nov-09 4:07
mveGerry Schmitz24-Nov-09 4:07 
QuestionUngroup shape in word file in c# Pin
Pankaj Saha23-Nov-09 2:24
Pankaj Saha23-Nov-09 2:24 
AnswerRe: Ungroup shape in word file in c# Pin
Pankaj Saha23-Nov-09 3:42
Pankaj Saha23-Nov-09 3:42 
GeneralRe: Ungroup shape in word file in c# Pin
Abhishek Sur23-Nov-09 3:48
professionalAbhishek Sur23-Nov-09 3:48 
GeneralRe: Ungroup shape in word file in c# Pin
Pankaj Saha23-Nov-09 17:46
Pankaj Saha23-Nov-09 17:46 
QuestionGetting CurrentException without catch. Pin
Paulo Zemek23-Nov-09 1:48
Paulo Zemek23-Nov-09 1:48 
AnswerRe: Getting CurrentException without catch. Pin
Richard MacCutchan23-Nov-09 2:06
mveRichard MacCutchan23-Nov-09 2:06 
AnswerRe: Getting CurrentException without catch. Pin
Luc Pattyn23-Nov-09 2:19
sitebuilderLuc Pattyn23-Nov-09 2:19 
AnswerRe: Getting CurrentException without catch. Pin
Shameel23-Nov-09 4:30
professionalShameel23-Nov-09 4:30 
GeneralRe: Getting CurrentException without catch. [modified] Pin
Paulo Zemek23-Nov-09 6:22
Paulo Zemek23-Nov-09 6:22 
GeneralRe: Getting CurrentException without catch. Pin
The Man from U.N.C.L.E.23-Nov-09 6:52
The Man from U.N.C.L.E.23-Nov-09 6:52 
GeneralRe: Getting CurrentException without catch. Pin
Paulo Zemek23-Nov-09 7:03
Paulo Zemek23-Nov-09 7:03 
GeneralRe: Getting CurrentException without catch. Pin
Shameel23-Nov-09 7:23
professionalShameel23-Nov-09 7:23 
AnswerRe: Getting CurrentException without catch. [modified] Pin
dojohansen23-Nov-09 8:25
dojohansen23-Nov-09 8:25 
Short answer: No.

There are sometimes ways to get an otherwise unhandled exception without a try-catch. In asp.net apps for example you can handle Application_OnError. But it's not a substitute for exception handling elsewhere in the code, only a last-resort chance to at least do things like log errors.

You can use either delegates or polymorphism if you want to standardize exception handling in certain situations. Something like this would be possible:

public static class DbHelper
{
   public static RunInTransaction(SqlConnection cnx, Action method)
   {
      cnx.Open();
      SqlTransaction tx = cnx.BeginTransaction();
      try
      {
        method();
        tx.Commit();
      }
      catch (Exception ex)
      {
        tx.Rollback();
        throw new ApplicationException("Error during database transaction.", ex);
      }
      finally
      {
        cnx.Close();
      }
   }
}



Here I used the predefined Action delegate, which is void and takes no parameters, but you could of course
use any delegate you'd like. Or you could use polymorphism: It could be an abstract method that derived classes must implement (obviously the class then can no longer be static!), or the method could accept an object implementing some interface instead of a delegate.

There are some cool possibilities with IDisposable but it is really intended specifically for early release of non-managed resources. It's not meant to be a general try-finally replacement, and certainly not a try-catch-finally replacement.

modified on Monday, November 23, 2009 2:39 PM

AnswerRe: Getting CurrentException without catch: use Marshal.GetExceptionCode() Pin
Daniel Grunwald23-Nov-09 13:07
Daniel Grunwald23-Nov-09 13:07 
GeneralRe: Getting CurrentException without catch: use Marshal.GetExceptionCode() Pin
Paulo Zemek23-Nov-09 13:12
Paulo Zemek23-Nov-09 13:12 
QuestionParameterless catch? Pin
Etienne_12323-Nov-09 0:20
Etienne_12323-Nov-09 0:20 
AnswerRe: Parameterless catch? Pin
dojohansen23-Nov-09 0:23
dojohansen23-Nov-09 0:23 
AnswerRe: Parameterless catch? Pin
Richard MacCutchan23-Nov-09 0:24
mveRichard MacCutchan23-Nov-09 0:24 
GeneralRe: Parameterless catch? Pin
Etienne_12323-Nov-09 0:29
Etienne_12323-Nov-09 0:29 
GeneralRe: Parameterless catch? Pin
dan!sh 23-Nov-09 0:35
professional dan!sh 23-Nov-09 0:35 
GeneralRe: Parameterless catch? Pin
dojohansen23-Nov-09 1:03
dojohansen23-Nov-09 1:03 
GeneralRe: Parameterless catch? Pin
Luc Pattyn23-Nov-09 1:10
sitebuilderLuc Pattyn23-Nov-09 1:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.