|
try..catch is so bourgeois.
cheers,
Chris Maunder
The Code Project | Co-founder
Microsoft C++ MVP
|
|
|
|
|
Chris Maunder wrote: since it's easy enough for us to avoid this with if statements
There is an IF statement, it just lacks the ELSE .
"The ones who care enough to do it right care too much to compromise."
Matthew Faithfull
|
|
|
|
|
An error condition returns the same value as a non-error condition? Many hamsters are dying because of that.
|
|
|
|
|
Oops. Typo.
cheers,
Chris Maunder
The Code Project | Co-founder
Microsoft C++ MVP
|
|
|
|
|
Hasn't T-SQL had try-catch since 2005?
|
|
|
|
|
if (then) else - Is this "too match" for SQL?
|
|
|
|
|
The problem with that view ("for consistency") is that you will forever be in the same programming mode. The form of the code should be
if @@error <> 0
BEGIN
Rollback Tran
Return 1
END
else
BEGIN
Commit Tran
Return 0
END
In my world there are no exceptions!
In C# I do accept the break statement within loops to allow an early escape. But no GOTOs. I programmed during the structured programming discussions in the 1970s. The amount of time required to comprehend GOTO filled code was enormous. And so easily fixed.
Gus Gustafson
|
|
|
|
|
Who uses it? N00bs.
DanielSheets wrote: It can make for cleaner code if used correctly. Do you have an example? After years in the trade, I have still to see the first instance where it actually improves readability/maintainability.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
public static void SaveChartData(List<DataGridClass> dataList)
{
int totalDelays = 0;
int value = 0;
string errorString = "";
string tmpFilePath = applicationPath + "TmpChartData.csv";
using (StreamWriter sw = new StreamWriter(tmpFilePath))
{
foreach (DataGridClass dgc in dataList)
{
if (!int.TryParse((dgc.MATL.Equals("") ? "0" : dgc.MATL), out value))
{
errorString = dgc.MATL;
goto ParseError;
}
totalDelays += value;
if (!int.TryParse((dgc.EQUIP.Equals("") ? "0" : dgc.EQUIP), out value))
{
errorString = dgc.EQUIP;
goto ParseError;
}
totalDelays += value;
if (!int.TryParse((dgc.People.Equals("") ? "0" : dgc.People), out value))
{
errorString = dgc.People;
goto ParseError;
}
totalDelays += value;
if (!int.TryParse((dgc.Defects.Equals("") ? "0" : dgc.Defects), out value))
{
errorString = dgc.Defects;
goto ParseError;
}
totalDelays += value;
if (!int.TryParse((dgc.Other.Equals("") ? "0" : dgc.Other), out value))
{
errorString = dgc.Other;
goto ParseError;
}
totalDelays += value;
sw.WriteLine("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10}",
dgc.Year, dgc.Month, dgc.Goal, dgc.Completions, totalDelays, dgc.to,
dgc.MATL, dgc.EQUIP, dgc.People, dgc.Defects, dgc.Other);
totalDelays = 0;
value = 0;
}
}
File.Copy(tmpFilePath, chartData, true);
return;
ParseError:
string msg = string.Format("Unable to parse data. Verify its entered correctly.\r\nValue = {0}", errorString);
MessageBox.Show(msg, "App Name", MessageBoxButton.OK, MessageBoxImage.Error);
}
|
|
|
|
|
Read up on exception-handling.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
For what part?
Why would I need to use exception handling when thats exactly what TryParse is used for (assuming thats what you're talking about)?
modified 1-Mar-13 11:10am.
|
|
|
|
|
..on how to throw an exception. I wouldn't even expect the "if's", but rather a loop; and have each condition in a separate class.
using (StreamWriter sw = new StreamWriter(tmpFilePath))
{
foreach (DataGridClass dgc in dataList)
{
foreach (var thingToTest in ParsesClasses)
{
Results.Add( thingToTest (dgc) );
}
Wrap it in a try-catch, and done.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Thats swift. I'll be the first to admit that I dont have the experience or knowledge of 90% of the people here. Interesting.
|
|
|
|
|
That looks bad to me. Aren't you supposed to use
break <label>; to jump out of loops?
Exception handling will usually bog down the handled block of code.
Q. Hey man! have you sorted out the finite soup machine?
A. Why yes, it's celery or tomato.
modified 1-Mar-13 11:34am.
|
|
|
|
|
Unfortunately that Java feature is not in C#.
|
|
|
|
|
Wow! what an oversight. It sure makes life easier.
Q. Hey man! have you sorted out the finite soup machine?
A. Why yes, it's celery or tomato.
|
|
|
|
|
dusty_dex wrote: Exception handling will usually bog down the handled block of code.
If that is a reference to performance then as stated it isn't true.
|
|
|
|
|
DanielSheets wrote: I'll be the first to admit that I dont have the experience or knowledge of 90% of the people here. The fact that you care about readability proves that you can't be in the bottom 10%
Readable code leads to maintainable projects. The better a project can be maintained, the more chances it will survive in the long run.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Eddy Vluggen wrote: Readable code leads to maintainable projects. The better a project can be maintained, the more chances it will survive in the long run.
.... which is completely offset by his use of 'goto'.
If it's not broken, fix it until it is
|
|
|
|
|
Kevin Marois wrote: .... which is completely offset by his use of 'goto'.
Nope.
The code as presented was readable and was maintainable.
Given that maintenance is a proven and known expense then I would much rather have the code that was presented versus some esoteric trick that is technically correct but neither readable nor maintainable.
|
|
|
|
|
That's horrible. Repeated code, and mixing of data management and UI code. It's crying out for throwing exceptions which you catch in the UI code, and a method
int parseNumericValue(String text){
int result;
if(!int.TryParse(text == "" ? "0" : text, out result))
throw new DataFormatException("Unable to parse data. Verify its entered correctly.\r\nValue = "+text);
return result;
}
(and that's if the default exception from Parse doesn't do the job which it probably does).
This is translated VB. And that's not a good thing.
|
|
|
|
|
Where do you see UI code in the snippet I posted?
|
|
|
|
|
|
|