Click here to Skip to main content
15,887,350 members
Home / Discussions / C#
   

C#

 
QuestionTranslating an "error event" to a managed exception Pin
Bernhard Hiller5-Mar-14 4:17
Bernhard Hiller5-Mar-14 4:17 
AnswerRe: Translating an "error event" to a managed exception Pin
Ravi Bhavnani5-Mar-14 4:25
professionalRavi Bhavnani5-Mar-14 4:25 
GeneralRe: Translating an "error event" to a managed exception Pin
OriginalGriff5-Mar-14 4:38
mveOriginalGriff5-Mar-14 4:38 
GeneralRe: Translating an "error event" to a managed exception Pin
Ravi Bhavnani5-Mar-14 4:46
professionalRavi Bhavnani5-Mar-14 4:46 
GeneralRe: Translating an "error event" to a managed exception Pin
OriginalGriff5-Mar-14 5:13
mveOriginalGriff5-Mar-14 5:13 
GeneralRe: Translating an "error event" to a managed exception Pin
BobJanova5-Mar-14 5:46
BobJanova5-Mar-14 5:46 
AnswerRe: Translating an "error event" to a managed exception Pin
Shameel5-Mar-14 5:07
professionalShameel5-Mar-14 5:07 
AnswerRe: Translating an "error event" to a managed exception Pin
BobJanova5-Mar-14 5:53
BobJanova5-Mar-14 5:53 
This could get really ugly. You must allow the method to complete, so the library gets its result back, but you'd like the managed code to die with an exception as soon as an error occurs.

What is the exact conditions under which this delegate can get called? Is it that any call to third party library code could case OnError to be called, and then your code will continue afterwards?

I don't really see an alternative to

Library.CallToSomething();
if(ErrorWasRaised) throw new MyException("...");

Library.CallToSomethingElse();
if(ErrorWasRaised) throw new MyException("...");


... where ErrorWasRaised is instance state that's assigned from your OnError handler.

The only thing I can think of is to hide the ugliness in a utility method with a lambda:

void CallErrorHandlingLibraryCode(Action code) {
 string errorId = null, errorMessage = null;
 OnErrorHandler errorLogger = (id, message) => {
  errorId = id; errorMessage = message;
  return true;
 };
 Library.OnError += errorLogger;
 try {
  code();
  if(null != errorId)
   throw new SASException(errorId, errorMessage);
 } finally {
  Library.OnError -= errorLogger;
 }
}


Then your calling code looks like

CallErrorHandlingLibraryCode(() => Library.CallToSomething());
CallErrorHandlingLibraryCode(() => Library.CallToSomethingElse());

... which is arguably better looking depending on your taste in lambdas.
AnswerExceptions are exceptional Pin
Ennis Ray Lynch, Jr.5-Mar-14 8:58
Ennis Ray Lynch, Jr.5-Mar-14 8:58 
AnswerRe: Translating an "error event" to a managed exception Pin
jschell5-Mar-14 9:41
jschell5-Mar-14 9:41 
GeneralRe: Translating an "error event" to a managed exception Pin
Bernhard Hiller5-Mar-14 20:17
Bernhard Hiller5-Mar-14 20:17 
QuestionPlease help me. don't working save button Pin
bayaa4-Mar-14 23:03
bayaa4-Mar-14 23:03 
SuggestionRe: Please help me. don't working save button Pin
Richard Deeming4-Mar-14 23:13
mveRichard Deeming4-Mar-14 23:13 
GeneralMessage Closed Pin
6-Mar-14 3:15
FesAlex6-Mar-14 3:15 
GeneralRe: Please help me. don't working save button Pin
Pete O'Hanlon6-Mar-14 3:32
mvePete O'Hanlon6-Mar-14 3:32 
GeneralRe: Please help me. don't working save button Pin
thatraja6-Mar-14 3:59
professionalthatraja6-Mar-14 3:59 
GeneralRe: Please help me. don't working save button Pin
bayaa6-Mar-14 16:09
bayaa6-Mar-14 16:09 
GeneralRe: Please help me. don't working save button Pin
bayaa6-Mar-14 16:10
bayaa6-Mar-14 16:10 
AnswerRe: Please help me. don't working save button Pin
Member 104046945-Mar-14 21:18
Member 104046945-Mar-14 21:18 
GeneralRe: Please help me. don't working save button Pin
bayaa6-Mar-14 15:55
bayaa6-Mar-14 15:55 
GeneralRe: Please help me. don't working save button Pin
bayaa6-Mar-14 19:29
bayaa6-Mar-14 19:29 
GeneralRe: Please help me. don't working save button Pin
bayaa6-Mar-14 19:52
bayaa6-Mar-14 19:52 
QuestionInheriting binding list Pin
Gilbert Consellado4-Mar-14 21:52
professionalGilbert Consellado4-Mar-14 21:52 
AnswerRe: Inheriting binding list Pin
BobJanova5-Mar-14 0:56
BobJanova5-Mar-14 0:56 
GeneralRe: Inheriting binding list Pin
Gilbert Consellado5-Mar-14 22:19
professionalGilbert Consellado5-Mar-14 22:19 

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.