Catching Exceptions

J

Jeffrey Walton

Hi All,

I derived an EncodingException from Exception. In my code, when I
catch an OverflowException I throw a new EncodingException. The catch
and new-throw executes fine.

However, the code I am trying to guard never catches an exceptions.
The guarded code is below. I've read Jeffrey Richter's chapter on
Exceptions in CLR via C#. This behaior is neither expected nor
documented. The EncodingException class will be hung off this thread.
Any ideas why no exceptions are being caught?

Thanks,
Jeff

******************************************
int Eaten = 0;
try
{
Eaten = TryParseNested(BytesToConsume);
}
catch (EncodingException ex)
{
// Eat this exception. We are only testing if the
// the Bit String or Octet String can be parsed.
// Since we are here, it apparently cannot be parsed.
// Return -1 to screw up the math. The bad math will be
// recognized in the caller.
Debug.Print(ex.Message);
return -1;
}

catch (OverflowException ex)
{
Debug.Print(ex.Message);
return -1;
}

catch( Exception ex)
{
Debug.Print(ex.Message);
return -1;
}
******************************************

And the TryNextParse:

******************************************
int Length = 0;
try
{
Length = GetLength();
}
catch (OverflowException overflow) // <== First Caught Here
{
throw new EncodingException(InternalOffset);
}
 
J

Jeffrey Walton

The derived exception is below. It is straight out of Richter's book.

*************************************
[Serializable]
public sealed class EncodingException : Exception, ISerializable
{
private int _Offset = 0;
public int Offset
{ get { return _Offset; } }

public override string Message
{
get
{
StringBuilder builder = new StringBuilder(base.Message);
builder.AppendFormat("Illegal encoding encountered at " +
"Offset {0}{1}", _Offset, Environment.NewLine);

return builder.ToString();
}
}

// Base Class
public EncodingException() : base() { }
public EncodingException(String message) : base(message) { }
public EncodingException(String message, Exception innerException)
: base(message, innerException) { }

public EncodingException(int offset) { _Offset = offset; }

public EncodingException(string message, int offset)
: this(message) { _Offset = offset; }

public EncodingException(string message, Exception innerException,
int offset)
: this(message, innerException) { _Offset = offset; }

public EncodingException(string message, int offset, Exception
innerException)
: this(message, innerException) { _Offset = offset; }

// Serialization
private EncodingException(SerializationInfo info, StreamingContext
context)
: base(info, context)
{
_Offset = info.GetInt32("Offset");
}

// Allow callers to access state of this object
[SecurityPermission(SecurityAction.Demand, SerializationFormatter =
true)]
public override void GetObjectData(SerializationInfo info,
StreamingContext context)
{
base.GetObjectData(info, context);
info.AddValue("Offset", _Offset);
}
}
 
M

Marc Gravell

Do you have a demonstrable example of this? i.e. where an exception
(of whatever type) is thrown and not caught?

Almost certainly just a typo, but your top code uses TryParseNested,
but your code with the re-throw is TryNextParse; are the two the
same / linked?

Also - what *does* happen? Nothing? An exception bubbles to the top?
What? Perhaps the exception happens on a pool thread, in which case
you're in trouble...

Finally - what framework? It is fixed in 2.0, but in 1.1 there is a
(very rarely-encountered) scenario where the "thing" that is thrown
(particularly by unmanaged code) does not derive from Exception, in
which case "catch (Exception) {...}" won't see it, and you need simply
"catch {...}" - and of course you can't get at the details... In 2.0
and above it gets wrapped automatically (by default) for you.

Marc
 
P

Peter Duniho

Hi All,

I derived an EncodingException from Exception. In my code, when I
catch an OverflowException I throw a new EncodingException. The catch
and new-throw executes fine.

However, the code I am trying to guard never catches an exceptions.
The guarded code is below.

Please post a concise-but-complete example of code that reliably
demonstrates the problem. IMHO, the code you posted is insufficient.

Here's an example that _would_ be "concise-but-complete", if it
actually reproduced the problem you seem to be describing (it
doesn't...it works exactly as one might expect, catching the new
exception that was thrown):


using System;



namespace TestExceptionRethrow

{

class Program

{

class MyException : Exception

{

}



static void Main(string[] args)

{

try

{

ThrowOne();

}

catch (MyException)

{

Console.WriteLine("Caught MyException");

}

Console.WriteLine("Done. Press Return to continue.");

Console.ReadLine();

}



static void ThrowOne()

{

byte[] rgb = new byte[1];



try

{

rgb[1] = 0;

}

catch (Exception)

{

throw new MyException();

}

}

}

}
 
L

Lasse Vågsæther Karlsen

Jeffrey said:
The derived exception is below. It is straight out of Richter's book.

*************************************
[Serializable]
public sealed class EncodingException : Exception, ISerializable

Is the exception defined in multiple places. For instance, the code that
throws it is in a class library, and have the exception defined in the
library, and the code catching it is in an application, and the
exception is defined there too.
 
J

Jeffrey Walton

HI All,

I wanted to report back on the behavior of the feature I found.

The basic problem was an exception of user defined type
EncodingException was not caught as expected in one source file. In
the other source files, I had to explicitly use <Namespace>.<Class>.
However, in this source file, the compile did not Error or Warn when
using just <ClassName> (EncodingException). But the exception went
uncaught.

When I changed From throw EncodingException to ASN1.EncodingException,
the exception was caught. This leads me to believe I may have collided
with another EncodingException (supplied by Microsoft?). But I was not
able to locate the culprit.

Jeff
 

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