Can assignment to a fundamental type throw an exception

  • Thread starter Thread starter MikeP
  • Start date Start date
M

MikeP

Hi there,

Can simply assigning a (valid) value to a fundamental .NET type (int, bool,
etc,. or even a reference) cause an exception in theory. I imagine it's
theoretically possible (not sure what the standard says) but so unlikely
that one need not worry about it in practice (assuming your app doesn't deal
with life and death issues). I'm really more curious abut the issue. Thanks.
 
Hi there,

Can simply assigning a (valid) value to a fundamental .NET type (int, bool,
etc,. or even a reference) cause an exception in theory. I imagine it's
theoretically possible (not sure what the standard says) but so unlikely
that one need not worry about it in practice (assuming your app doesn't deal
with life and death issues). I'm really more curious abut the issue. Thanks.

I believe that if the types are identical, no exception could occur.
But if anything at all other than a straight bit-for-bit copy has to
occur, there could be an exception.

For example:

long x = 5000000000;
int y = (int)x;

There's an explicit cast there, but there are situations where that
cast might be implicitly defined (I don't think that one should write
implicit casts where an exception might occur, but AFAIK the language
doesn't prohibit that).

Pete
 
I believe that if the types are identical, no exception could occur. But
if anything at all other than a straight bit-for-bit copy has to occur,
there could be an exception.

For example:

long x = 5,000,000,000;
int y = (int)x;

There's an explicit cast there, but there are situations where that cast
might be implicitly defined (I don't think that one should write implicit
casts where an exception might occur, but AFAIK the language doesn't
prohibit that).

Thanks for the reply. I'm really confining my question to legal assignments
using valid values only (over/underflow not an issue nor exception-throwing
casts). I would think that no exception could occur as you suggested but
wonder if it's possible at all. For instance, the fact that all these types
are really aliases for native .NET structures seems to cloud the issue. Am I
to understand that each fundamental type is actually a structure behind the
scenes or does that only occur under certain circumstances (like when
boxing). If the structure does come into play during simple assignments
(which seems bizarre to me), can't these structures then throw an exception?
 
[...] For instance, the fact that all these types
are really aliases for native .NET structures seems to cloud the issue. Am I
to understand that each fundamental type is actually a structure behind the
scenes or does that only occur under certain circumstances (like when
boxing).

I'm not really sure. I'd have to go back and re-read the specifics in
the C# spec to answer that question. However, it's certainly true that
the fundamental types have many of the same features as regular structs
(including that they have defined methods on the types).
If the structure does come into play during simple assignments
(which seems bizarre to me), can't these structures then throw an exception?

Sure...any time you execute a method, that method could include code
that throws an exception.

But that doesn't mean that all methods include code that throws an
exception, and it also doesn't mean that assignments necessarily invoke
methods. Note in particular that you cannot overload the = operator,
and as far as I know the built-in = operator simply copies value types.

Which is the basic source of my original answer: if something else
happens in an assignment, like a typecast, the potential for an
exception does exist. But the assignment itself could not cause an
exception; it's only copying data.

Pete
 
If the structure does come into play during simple assignments
Sure...any time you execute a method, that method could include code that
throws an exception.

But that doesn't mean that all methods include code that throws an
exception, and it also doesn't mean that assignments necessarily invoke
methods. Note in particular that you cannot overload the = operator, and
as far as I know the built-in = operator simply copies value types.

Ok, thanks. It seems very unlikely that operator = would throw an exception
for the basic types even if some high-level function were being called for
it (especially in a release build but probably debug also). I'm still not
100% sure if it's still possible however but as I said previously, I'm not
concerned that it would ever happen even if it could (what are the odds).
I'm just assuming that the basic operations on fundamental types are almost
certainly handled without incurring the overhead of some high-level
structure (whether present or not). They would have to be for obvious
performance reasons, in particular where the native hardware can handle
things unassisted (and in this case an exception at the language level seems
extremely implausible)
 

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

Back
Top