Click here to Skip to main content
15,895,606 members
Home / Discussions / C#
   

C#

 
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
mvaPaulo 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
mvaPaulo 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
mvaPaulo Zemek23-Nov-09 13:12 
QuestionParameterless catch? Pin
Etienne_12323-Nov-09 0:20
Etienne_12323-Nov-09 0:20 
AnswerRe: Parameterless catch? PinPopular
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 
GeneralRe: Parameterless catch? Pin
Richard MacCutchan23-Nov-09 2:04
mveRichard MacCutchan23-Nov-09 2:04 
GeneralRe: Parameterless catch? Pin
Eddy Vluggen23-Nov-09 2:21
professionalEddy Vluggen23-Nov-09 2:21 
GeneralRe: Parameterless catch? Pin
Saksida Bojan23-Nov-09 6:15
Saksida Bojan23-Nov-09 6:15 
GeneralRe: Parameterless catch? Pin
Eddy Vluggen23-Nov-09 7:46
professionalEddy Vluggen23-Nov-09 7:46 
GeneralRe: Parameterless catch? Pin
OriginalGriff23-Nov-09 4:13
mveOriginalGriff23-Nov-09 4:13 
GeneralRe: Parameterless catch? Pin
Luc Pattyn23-Nov-09 10:10
sitebuilderLuc Pattyn23-Nov-09 10:10 
AnswerRe: Parameterless catch? Pin
Shameel23-Nov-09 2:05
professionalShameel23-Nov-09 2:05 
AnswerRe: Parameterless catch? Pin
Christian Graus23-Nov-09 2:21
protectorChristian Graus23-Nov-09 2:21 

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.