Try/catch block...
-
The funniest of all, is that when I was programming that, that I didn't know why I got an error sometimes...
ProgramFOX
Maybe the file was in use :laugh:
-
I found this in one of my old codes:
public string ReadFile(string filename)
{
try
{
return File.ReadAllText(filename);
}
catch
{
return File.ReadAllText(filename);
}
}:doh:
ProgramFOX
-
When I was programming that, I thought: "It's in a catch block, then it can't throw an error!" X|
ProgramFOX
-
I'd do it three times, because three is the magic number:
public string ReadFile(string filename)
{
try
{
return File.ReadAllText(filename);
}
catch
{
return File.ReadAllText(filename);
}
finally
{
return File.ReadAllText(filename);
}
}Sigh.
public string ReadFile(string filename)
{
string contents = null;
try
{
contents = File.ReadAllText(filename);
}
catch
{
contents = File.ReadAllText(filename);
}
finally
{
contents = File.ReadAllText(filename);
}
return contents;
}Okay. Everybody happy now?
Software Zen:
delete this;
-
I found this in one of my old codes:
public string ReadFile(string filename)
{
try
{
return File.ReadAllText(filename);
}
catch
{
return File.ReadAllText(filename);
}
}:doh:
ProgramFOX
Mmmm... you seem determined to read that file, is it important?
CEO at: - Rafaga Systems - Para Facturas - Modern Components for the moment...
-
Mmmm... you seem determined to read that file, is it important?
CEO at: - Rafaga Systems - Para Facturas - Modern Components for the moment...
No. The reading of the file isn't important. That's a code from 2 years ago. Now, I know how I can read files!
ProgramFOX
-
Really? :omg: Which value would expect to be returned here?
try
{
return 1;
}
catch
{
return 2;
}
finally
{
return 42;
}I think the C# compiler is doing the right thing. Either the
return
in thefinally
block is ignored, in which case it shouldn't be allowed, or it replaces any value returned from thetry
orcatch
blocks, which is just confusing.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
I found this in one of my old codes:
public string ReadFile(string filename)
{
try
{
return File.ReadAllText(filename);
}
catch
{
return File.ReadAllText(filename);
}
}:doh:
ProgramFOX
-
I'd do it three times, because three is the magic number:
public string ReadFile(string filename)
{
try
{
return File.ReadAllText(filename);
}
catch
{
return File.ReadAllText(filename);
}
finally
{
return File.ReadAllText(filename);
}
}Quitter - use recursion!
public string ReadFile(string filename)
{
try
{
return File.ReadAllText(filename);
}
catch
{
return ReadFile(filename);
}
}Life is like a s**t sandwich; the more bread you have, the less s**t you eat.
-
I'd do it three times, because three is the magic number:
public string ReadFile(string filename)
{
try
{
return File.ReadAllText(filename);
}
catch
{
return File.ReadAllText(filename);
}
finally
{
return File.ReadAllText(filename);
}
}Maybe we need a more flippant programming language, for those of us who think like this:
try
{
// something
}
ifatfirstyoudontsucceed
{
// try again
}
thirdtimelucky
{
// and again
}
giveup // synonym for catch
{
// error handling
}
finally
{
// tidying up
} -
You would be more likely to get that right if you added commentary: try { return 1; // Genesis } catch { return 22; // Joseph Heller ] finally { return 42; // Douglas Adams }
-
You would be more likely to get that right if you added commentary: try { return 1; // Genesis } catch { return 22; // Joseph Heller ] finally { return 42; // Douglas Adams }
Oh yes you can do so. But there is a catch - it's "catch 22". Why did it take me so long to see it?
-
You would be more likely to get that right if you added commentary: try { return 1; // Genesis } catch { return 22; // Joseph Heller ] finally { return 42; // Douglas Adams }
-
If that's C#, it won't compile; you'll get a "Control cannot leave the body of a finally clause" compiler error.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Richard Deeming wrote:
If that's C#, it won't compile; you'll get a "Control cannot leave the body of a finally clause" compiler error.
I had to play around. You can throw an error in the try/catch/finally segments and it will compile. Since I didn't enclose the routine in a try catch, the finally segment did blow up with an unhandled exception. Then I did put it in that logic:
try { int junk = trycat(); } catch (Exception ex) { Console.WriteLine("caught exception {0}", ex.Message); Console.ReadKey(); }
...
static int trycat() { try { throw (new Exception("Throwing an exception in try block")); } catch { throw (new Exception("Throwing an exception in catch block")); } finally { throw (new Exception("Throwing an exception in finally block")); } return 1; }
-
Quitter - use recursion!
public string ReadFile(string filename)
{
try
{
return File.ReadAllText(filename);
}
catch
{
return ReadFile(filename);
}
}Life is like a s**t sandwich; the more bread you have, the less s**t you eat.
brisingr@gryphon-pc$ cpmsg --process --msg "4433493" --forum "The Weird and The Wonderful" --code "Process" ERROR: StackOverflowException was encountered. brisingr@gryphon-pc$|
Bob Dole
The internet is a great way to get on the net.
:doh: 2.0.82.7292 SP6a
-
I'd do it three times, because three is the magic number:
public string ReadFile(string filename)
{
try
{
return File.ReadAllText(filename);
}
catch
{
return File.ReadAllText(filename);
}
finally
{
return File.ReadAllText(filename);
}
}Bernhard Hiller wrote:
three is the magic number
It sure is! Monty Python : First shalt thou take out the Holy Pin. Then, shalt thou count to three, no more, no less. Three shalt be the number thou shalt count, and the number of the counting shall be three. Four shalt thou not count, nor either count thou two, excepting that thou then proceed to three. Five is right out. Once the number three, being the third number, be reached, then lobbest thou thy Holy Hand Grenade of Antioch towards thy foe, who, being naughty in my sight, shall snuff it."
Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson
-
Maybe the file was in use :laugh: