473,385 Members | 1,555 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Internal Error and enums

When I'm coding in Java, I might write the following (contrived) code:

boolean b = SomeMethod();
switch (b) {
case true:
return 1;
case false:
return 0;
default:
throw new InternalError();
}

InternalError indicates that something has gone massively wrong with
either the compiler or the virtual machine.

In C#, the avaliability of enums means that this seems more vital.

enum Foobar {
ONE,
TWO,
THREE
}
....
Foobar foobar = SomeMethod();
switch (foobar) {
case Foobar.ONE:
return 1;
case Foobar.TWO:
return 2;
case Foobar.THREE:
return 3;
default:
//What do I put here?
}

I could just change case "Foobar.THREE:" to "default:", but that
reduces readability.

I've been looking for an equivalent of InternalError in C#, but can't
find one in the standard. What do other people do in a situation like
this?

Alun Harford

Nov 15 '06 #1
7 2189
al*********@yahoo.com wrote:
When I'm coding in Java, I might write the following (contrived) code:

boolean b = SomeMethod();
switch (b) {
case true:
return 1;
case false:
return 0;
default:
throw new InternalError();
}

InternalError indicates that something has gone massively wrong with
either the compiler or the virtual machine.

In C#, the avaliability of enums means that this seems more vital.

enum Foobar {
ONE,
TWO,
THREE
}
...
Foobar foobar = SomeMethod();
switch (foobar) {
case Foobar.ONE:
return 1;
case Foobar.TWO:
return 2;
case Foobar.THREE:
return 3;
default:
//What do I put here?
}

I could just change case "Foobar.THREE:" to "default:", but that
reduces readability.

I've been looking for an equivalent of InternalError in C#, but can't
find one in the standard. What do other people do in a situation like
this?
You need to handle it as it would be possible to assign any int value to
foobar. It is an enum, but the underlying type by default for enum is int.
So you can assign a value of 4. For example, the body of SomeMethod could
be:

return (Foobar)4;

This would compile and run fine and hit your default. In your instance you
might consider throwing either a System.ArgumentException or
System.ComponentModel.InvalidEnumArgumentException .
--
Tom Porterfield

Nov 15 '06 #2

Tom Porterfield wrote:
al*********@yahoo.com wrote:
<snip>
I've been looking for an equivalent of InternalError in C#, but can't
find one in the standard. What do other people do in a situation like
this?

You need to handle it as it would be possible to assign any int value to
foobar. It is an enum, but the underlying type by default for enum is int.
So you can assign a value of 4.
Good point. The problem can still occur though. Take the following
code:

switch((byte)foo) {
case 0:
return 0;
break;
case 1:
return 1;
break;
...
...
case 254:
return 254;
break;
case 255:
return 255;
break;
default:
//What here?
}

Alun Harford

Nov 15 '06 #3
<al*********@yahoo.comwrote:
When I'm coding in Java, I might write the following (contrived) code:

boolean b = SomeMethod();
switch (b) {
case true:
return 1;
case false:
return 0;
default:
throw new InternalError();
}
Except that Java doesn't let you switch on booleans :)
InternalError indicates that something has gone massively wrong with
either the compiler or the virtual machine.

In C#, the avaliability of enums means that this seems more vital.

enum Foobar {
ONE,
TWO,
THREE
}
...
Foobar foobar = SomeMethod();
switch (foobar) {
case Foobar.ONE:
return 1;
case Foobar.TWO:
return 2;
case Foobar.THREE:
return 3;
default:
//What do I put here?
}

I could just change case "Foobar.THREE:" to "default:", but that
reduces readability.

I've been looking for an equivalent of InternalError in C#, but can't
find one in the standard. What do other people do in a situation like
this?
InvalidOperationException is often appropriate:
"The exception that is thrown when a method call is invalid for the
object's current state."

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 15 '06 #4
al*********@yahoo.com wrote:
>
Tom Porterfield wrote:
>al*********@yahoo.com wrote:
<snip>
I've been looking for an equivalent of InternalError in C#, but can't
find one in the standard. What do other people do in a situation like
this?

You need to handle it as it would be possible to assign any int value to
foobar. It is an enum, but the underlying type by default for enum is
int. So you can assign a value of 4.

Good point. The problem can still occur though. Take the following
code:

switch((byte)foo) {
case 0:
return 0;
break;
case 1:
return 1;
break;
...
...
case 254:
return 254;
break;
case 255:
return 255;
break;
default:
//What here?
}

Alun Harford
Hi Alun,

I think if, in the cases you are describing, the default case is ever
reached, then any code you put in there won't be executed anyway, or the
exceution will be undefined, because the CLI has more than likely given up.

--
Hope this helps,
Tom Spink

Google first, ask later.
Nov 16 '06 #5
On 15 Nov 2006 14:18:30 -0800, al*********@yahoo.com wrote:
Good point. The problem can still occur though. Take the following
code:

switch((byte)foo) {
case 0:
return 0;
break;
case 1:
return 1;
break;
...
...
case 254:
return 254;
break;
case 255:
return 255;
break;
default:
//What here?
}
Same answer as before, throw an exception with relevant information. There
isn't an equivalent to java's InternalError, but you could use
InvalidOperationException, InvalidEnumArgumentException or
InvalidArgumentException. BTW, you don't need the break if you are
returning directly from within the case.
--
Tom Porterfield
Nov 16 '06 #6
In the "for what it's worth department", MS (online documentation, at
least) recommends that one's application generate an
ApplicationException. Of course you could inherit from this class and
invent your own exceptions. We do this. The benefit, if it is indeed a
benefit, is that one can intercept the application-generated exceptions
explicitly.
On 2006-11-15 16:44:20 -0600, Jon Skeet [C# MVP] <sk***@pobox.comsaid:
<al*********@yahoo.comwrote:
>When I'm coding in Java, I might write the following (contrived) code:

boolean b = SomeMethod();
switch (b) {
case true:
return 1;
case false:
return 0;
default:
throw new InternalError();
}

Except that Java doesn't let you switch on booleans :)
>InternalError indicates that something has gone massively wrong with
either the compiler or the virtual machine.

In C#, the avaliability of enums means that this seems more vital.

enum Foobar {
ONE,
TWO,
THREE
}
...
Foobar foobar = SomeMethod();
switch (foobar) {
case Foobar.ONE:
return 1;
case Foobar.TWO:
return 2;
case Foobar.THREE:
return 3;
default:
//What do I put here?
}

I could just change case "Foobar.THREE:" to "default:", but that
reduces readability.

I've been looking for an equivalent of InternalError in C#, but can't
find one in the standard. What do other people do in a situation like
this?

InvalidOperationException is often appropriate:
"The exception that is thrown when a method call is invalid for the
object's current state."

Nov 16 '06 #7
Bob Jones <ro****@joneshouse.comwrote:
In the "for what it's worth department", MS (online documentation, at
least) recommends that one's application generate an
ApplicationException. Of course you could inherit from this class and
invent your own exceptions. We do this. The benefit, if it is indeed a
benefit, is that one can intercept the application-generated exceptions
explicitly.
That used to be the case - but now ApplicationException is reckoned
(even by MS) to be a bad idea, as it doesn't add much other than an
extra level of hierarchy.

Note that it's always been fine to "reuse" system exceptions where they
accurately describe the situation - it's not worth reinventing the
wheel, just because it happens to be an application that generates the
exception.

See http://blogs.msdn.com/kcwalina/archi...23/644822.aspx for a
bit more.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 16 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: SpaceCowboy | last post by:
I recently got into a discussion with a co-worker about using enums across a dll interface. He wanted to use chars instead, argueing that depending on compiler settings the size of an enum could...
2
by: Faisal | last post by:
Can anyone tell me if it is possible to enumerate through all the Enums within a class . I have a class with many Enums and would like to accees the Enums through an array/collection etc. I can't...
1
by: Ayende Rahien | last post by:
reparing resources... Updating references... Performing main compilation... error CS0583: Internal Compiler Error (0xc0000005 at address 53168B12): likely culprit is 'BIND'. An internal...
4
by: David Sworder | last post by:
Consider the following line of code (it's not important what it does): resp.DocItem=Relations.SelectDocItems_BySearchString(req.SearchPhrase); It turns out that this line is in error. The...
4
by: Martin Pritchard | last post by:
Hi, I'm working on a project that historically contains around 40 enums. In the database various fields refer to the int values of these enums, but of course ref integrity is not enofrced and...
8
by: Rod | last post by:
I have been working with ASP.NET 1.1 for quite a while now. For some reason, opening some ASP.NET applications we wrote is producing the following error message: "The Web server reported...
2
by: mike_li | last post by:
On Window 2000 Professional Server DB2 UDB Level: DB2 code release "SQL07029" with level identifie "030A0105" and informational tokens "DB2 v7.1.0.98", "n040510" and "WR21337". In the...
9
by: dylan.miller | last post by:
I'm having trouble understanding the internal access modifier. There are many classes in my assembly that should not be accessible outside of the assembly. I've used the internal access modifier...
35
by: =?Utf-8?B?UElFQkFMRA==?= | last post by:
I'd really like to be able to constrain a generic type to System.Enum or, better, enum. But of course that results in "Compiler Error CS0702". So far I've been checking the type parameter at...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.