Exception not being caught

H

hardieca

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.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

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.

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?
 
B

Ben Voigt [C++ MVP]

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?

Maybe the delegate is invoked from a worker thread?
 
H

hardieca

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
 
B

Ben Voigt [C++ MVP]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top