|
It at least recognises the futility: as can't throw an exception, it returns a null instead...
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
|
|
|
|
|
I know that there are a number of programers that don't understand proper Exception Handling, but this one is really bad. Maybe he was thinking that he should retry? Even then it is not done properly.
Just because the code works, it doesn't mean that it is good code.
|
|
|
|
|
yeahhh.. it won't go in catch block ever.
R A M
|
|
|
|
|
If at first you don't succeed, try, try, try throw, throw, throw again. Or something like that.
Chris Meech
I am Canadian. [heard in a local bar]
In theory there is no difference between theory and practice. In practice there is. [Yogi Berra]
|
|
|
|
|
|
I have an improved version:
Page executingPage = null;
while (true)
{
try
{
executingPage = HttpContext.Current.Handler as Page;
break;
}
catch (InvalidCastException ex)
{
continue;
}
}
|
|
|
|
|
Yep, this should work MUCH better
____________________________________________________________
Be brave little warrior, be VERY brave
|
|
|
|
|
LOL!!! Oh man.. you just made my week.
|
|
|
|
|
lol then what was the use of the variable "ex" he simple don't understand exception handling
Vuyiswa Maseko,
Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code.
C#/VB.NET/ASP.NET/SQL7/2000/2005/2008
http://www.vuyiswamaseko.com
vuyiswa@its.co.za
http://www.itsabacus.co.za/itsabacus/
|
|
|
|
|
DWC - (Zero tolerance for) drunk while coding...
|
|
|
|
|
That's GOT to be a DWC!!
|
|
|
|
|
I'm always more productive when drunk.
(Off-topic)
But yeah, he was probably drunk.
|
|
|
|
|
I once fixed something similar that was coded by a newbie, it went
something like this:
private void HandleException(Exception ex)
{
try
{
... Code to handle exception that would always throw an exception
... Can't recall exactly what it was since it was 3 years ago
}
catch (Exception ex)
{
HandleException(ex);
}
}
The function would recursively call itself and cause a stack overflow exception.
|
|
|
|
|
I think it is a waste of code
|
|
|
|
|
This is a piece of code i took from a book i read about java game development.
public boolean displayModesMatch(DisplayMode mode1,
DisplayMode mode2)
{
if (mode1.getWidth() != mode2.getWidth() ||
mode1.getHeight() != mode2.getHeight())
{
return false;
}
if (mode1.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI &&
mode2.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI &&
mode1.getBitDepth() != mode2.getBitDepth())
{
return false;
}
if (mode1.getRefreshRate() !=
DisplayMode.REFRESH_RATE_UNKNOWN &&
mode2.getRefreshRate() !=
DisplayMode.REFRESH_RATE_UNKNOWN &&
mode1.getRefreshRate() != mode2.getRefreshRate())
{
return false;
}
return true;
}
|
|
|
|
|
I remember seeing very much the same thing in some DirectX sample. It certainly is not the greatest code ever written, but where do you see the horror?
A while ago he asked me what he should have printed on my business cards. I said 'Wizard'.
I read books which nobody else understand. Then I do something which nobody understands. After that the computer does something which nobody understands. When asked, I say things about the results which nobody understand. But everybody expects miracles from me on a regular basis. Looks to me like the classical definition of a wizard.
|
|
|
|
|
The code will work of course, but it should be obvious that the if structures are redundant... simply return the combined value of all the booleans
|
|
|
|
|
Really? It would become quite an ugly expression and it would be much harder to read. I remember writing similar code when playing with DirectX, but I wanted to filter display modes out of a list and any parameter could also have a 'don't care' value. This would have gotten even messier, so I also went for the more readable variant.
A while ago he asked me what he should have printed on my business cards. I said 'Wizard'.
I read books which nobody else understand. Then I do something which nobody understands. After that the computer does something which nobody understands. When asked, I say things about the results which nobody understand. But everybody expects miracles from me on a regular basis. Looks to me like the classical definition of a wizard.
|
|
|
|
|
uhum that makes sence, but i'd probably try to simplify the expression using some describing variable names.
|
|
|
|
|
When putting together a graphics engine, you first have to set up a rendering context of some kind. To do that you will have to sort out what kind of graphics capabilities the host computer has and which not. In short: You will be writing lots of such code and there will be a few errors. Then it's really nice if you don't have to pull apart more complex logical terms and just can make your correction at the right place. It's just more easy to read and to maintain. And also the day will come when you have to look at the code and don't quite remember why you once decided to do things just the way they are, much less a stranger who works with your code for the first time.
A while ago he asked me what he should have printed on my business cards. I said 'Wizard'.
I read books which nobody else understand. Then I do something which nobody understands. After that the computer does something which nobody understands. When asked, I say things about the results which nobody understand. But everybody expects miracles from me on a regular basis. Looks to me like the classical definition of a wizard.
|
|
|
|
|
I believe i understand what you are saying, but i don't think the above solution is a good solution to that problem.
|
|
|
|
|
It sure does its job, wich itself is not really complicated. So why don't you just post your more fitting version?
A while ago he asked me what he should have printed on my business cards. I said 'Wizard'.
I read books which nobody else understand. Then I do something which nobody understands. After that the computer does something which nobody understands. When asked, I say things about the results which nobody understand. But everybody expects miracles from me on a regular basis. Looks to me like the classical definition of a wizard.
|
|
|
|
|
I'd probably go for this approach
public boolean displayModesMatch(DisplayMode mode1,
DisplayMode mode2)
{
boolean isEqualDimention = (mode1.getWidth() == mode2.getWidth() && mode1.getHeight() == mode2.getHeight());
boolean isEqualDepth = (mode1.getBitDepth() == mode2.getBitDepth());
boolean isEqualRefreshRate = (mode1.getRefreshRate() == mode2.getRefreshRate());
return (isEqualDimention && isEqualDepth && isEqualRefreshRate);
}
I think this is much more readable and much simpler.
|
|
|
|
|
Nice and well, but what happened to the BIT_DEPTH_MULTI and REFRESH_RATE_UNKNOWN business? Including those two will take away some of the neatness again. And what if we don't just want to check three things? What if there are 20? Then you will have 20 variables which are used in one fat term at the end.
A while ago he asked me what he should have printed on my business cards. I said 'Wizard'.
I read books which nobody else understand. Then I do something which nobody understands. After that the computer does something which nobody understands. When asked, I say things about the results which nobody understand. But everybody expects miracles from me on a regular basis. Looks to me like the classical definition of a wizard.
|
|
|
|
|
I believe BIT_DEPTH_MULTI and REFRESH_RATE_UNKNOWN don't need to be there. they are constants. even if they should be there, it still would be a lot cleaner. I'm not saying this IS the best approach i'm just saying how i think is the cleanest solution.
Even with 50 things to check it's much more readable using variables then doing some if structures :p
|
|
|
|