Hi,
I have a custom exception that is not being caught properly. I have
added a reference to a .NET wrapper DLL from HTML Tidy to my project.
When HTML Tidy throws a fatal error, I want my custom exception thrown
and caught so that I may write it so I can write it to a log file. I
am certain everything has been installed correctly, and the program
works beautifully except for the HTML Tidy exceptions not being caught
I am registering the method that throws my custom exception to a
delegate, could that cause issues? The strange thing is when I put a
breakpoint on the line that throws the exception, I can see that it's
being executed. I also see in my output windows that "A first chance
exception of type 'HTMLParser.HTMLParserException' occurred" but it's
not being caught.
The code:
//Simple exception
public class HTMLParserException : ApplicationException
{
public HTMLParserException() { }
public HTMLParserException(string message) : base(message) { }
public HTMLParserException(string message, Exception
innerEx) : base(message, innerEx) { }
}
public class Migrator{
public void TidyDiagnostics(TidyATL.TidyReportLevel level, int line,
int col, string message)
{
//THIS LINE IS EXECUTED WHEN doc.ParseString IS CALLED
throw new
HTMLParser.HTMLParserException(String.Format("{3}: {0} Line: {1}
Col: {2}", message, line, col, level));
}
void tidyUp(string html, string path)
{
Tidy.Document doc = new Tidy.Document();
doc.OnMessage += new
Tidy.IDocumentEvents_OnMessageEventHandler(TidyDiagnostics);
doc.SetOptBool(TidyATL.TidyOptionId.TidyXhtmlOut, 1);
doc.SetErrorFile("c:\\error.txt");
try
{
//ParseString causes an error, TidyDiagnostics is
called
doc.ParseString(html);
doc.CleanAndRepair();
}
catch (Exception e)
{
//THIS PART NEVER GETS EXECUTED
MessageBox.Show(e.Message);
}
Does anyone have any idea?
Thanks,
C.
Göran Andersson - 16 Oct 2007 20:45 GMT
> Hi,
>
[quoted text clipped - 63 lines]
>
> C.
Are you sure that the TidyDiagnostics method is called from ParseString?
You don't have any error handling in ParseString that catches the
exception before exiting the method?
Have you set a breakpoint in the catch, so that you are sure that this
point is never reached?

Signature
Göran Andersson
_____
http://www.guffa.com
Ben Voigt [C++ MVP] - 16 Oct 2007 23:10 GMT
> Hi,
>
[quoted text clipped - 59 lines]
>
> Does anyone have any idea?
Maybe the delegate is invoked from a worker thread?
> Thanks,
>
> C.
hardieca@hotmail.com - 17 Oct 2007 13:12 GMT
Hi Ben,
If the delegate is indeed invoked from a worker thread, how would I
catch the exception thrown in a method in its invocation list?
Chris
> <hardi...@hotmail.com> wrote in message
>
[quoted text clipped - 69 lines]
>
> > C.
Ben Voigt [C++ MVP] - 17 Oct 2007 18:05 GMT
> Hi Ben,
>
> If the delegate is indeed invoked from a worker thread, how would I
> catch the exception thrown in a method in its invocation list?
In that case, you wouldn't use an exception at all. Exceptions are used for
passing failure information to a caller... but there's no caller/callee
relationship between different threads. Instead, post a message back to
your main thread. One way is using Control.BeginInvoke for some control or
form passing a delegate which will perform the error handling, update the
UI, etc.
> Chris
>
[quoted text clipped - 71 lines]
>>
>> > C.