PC Review


Reply
Thread Tools Rate Thread

Catching Exceptions

 
 
Jeffrey Walton
Guest
Posts: n/a
 
      23rd Nov 2007
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);
}

 
Reply With Quote
 
 
 
 
Jeffrey Walton
Guest
Posts: n/a
 
      23rd Nov 2007
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);
}
}
 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      23rd Nov 2007
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
 
Reply With Quote
 
Peter Duniho
Guest
Posts: n/a
 
      23rd Nov 2007
On 2007-11-22 17:57:16 -0800, Jeffrey Walton <(E-Mail Removed)> said:

> 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();

}

}

}

}

 
Reply With Quote
 
Lasse Vågsæther Karlsen
Guest
Posts: n/a
 
      23rd Nov 2007
Jeffrey Walton wrote:
> 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.

--
Lasse Vågsæther Karlsen
private.php?do=newpm&u=
http://presentationmode.blogspot.com/
 
Reply With Quote
 
Jeffrey Walton
Guest
Posts: n/a
 
      25th Nov 2007
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

On Nov 22, 8:57 pm, Jeffrey Walton <noloa...@gmail.com> wrote:
> 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
>
> [SNIP ]

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Catching exceptions Andy B. Microsoft C# .NET 3 16th Mar 2010 10:33 AM
catching exceptions bitshift Microsoft Dot NET Framework 4 31st Aug 2007 10:51 AM
Catching CDatabase Exceptions? Mike C# Microsoft VC .NET 9 16th Jun 2006 01:15 AM
catching Exceptions at a top level Laxmikant Rashinkar Microsoft C# .NET 4 15th Feb 2005 07:41 PM
Re: Centrelized Exceptions Catching? John Vottero Microsoft C# .NET 0 6th Jul 2004 03:39 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:04 PM.