Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. Try/catch block...

Try/catch block...

Scheduled Pinned Locked Moved The Weird and The Wonderful
32 Posts 23 Posters 30 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Thomas Daniels

    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

    P Offline
    P Offline
    PIEBALDconsult
    wrote on last edited by
    #10

    It lacks a while.

    1 Reply Last reply
    0
    • T Thomas Daniels

      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

      J Offline
      J Offline
      jasperp
      wrote on last edited by
      #11

      I like it. Another opportunity to bill the client.

      1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        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

        A Offline
        A Offline
        acomputerdog
        wrote on last edited by
        #12

        That would compile on java, I think.

        Richard DeemingR 1 Reply Last reply
        0
        • A acomputerdog

          That would compile on java, I think.

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #13

          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 the finally block is ignored, in which case it shouldn't be allowed, or it replaces any value returned from the try or catch 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

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          A R B 3 Replies Last reply
          0
          • Richard DeemingR Richard Deeming

            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 the finally block is ignored, in which case it shouldn't be allowed, or it replaces any value returned from the try or catch 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

            A Offline
            A Offline
            acomputerdog
            wrote on last edited by
            #14

            Oh I wasn't saying it should be allowed, but I think it is.

            1 Reply Last reply
            0
            • Richard DeemingR Richard Deeming

              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 the finally block is ignored, in which case it shouldn't be allowed, or it replaces any value returned from the try or catch 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

              R Offline
              R Offline
              Reese Currie
              wrote on last edited by
              #15

              FTSOTR, that does compile in Java (with a little syntactic fix) and returns 42.

              1 Reply Last reply
              0
              • T Thomas Daniels

                The funniest of all, is that when I was programming that, that I didn't know why I got an error sometimes...

                ProgramFOX

                _ Offline
                _ Offline
                _Vitor Garcia_
                wrote on last edited by
                #16

                Maybe the file was in use :laugh:

                _ 1 Reply Last reply
                0
                • T Thomas Daniels

                  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

                  S Offline
                  S Offline
                  spencepk
                  wrote on last edited by
                  #17

                  Huh... why bother?

                  T 1 Reply Last reply
                  0
                  • S spencepk

                    Huh... why bother?

                    T Offline
                    T Offline
                    Thomas Daniels
                    wrote on last edited by
                    #18

                    When I was programming that, I thought: "It's in a catch block, then it can't throw an error!" X|

                    ProgramFOX

                    1 Reply Last reply
                    0
                    • B Bernhard Hiller

                      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);
                      }
                      }

                      G Offline
                      G Offline
                      Gary R Wheeler
                      wrote on last edited by
                      #19

                      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;

                      1 Reply Last reply
                      0
                      • T Thomas Daniels

                        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

                        R Offline
                        R Offline
                        RafagaX
                        wrote on last edited by
                        #20

                        Mmmm... you seem determined to read that file, is it important?

                        CEO at: - Rafaga Systems - Para Facturas - Modern Components for the moment...

                        T 1 Reply Last reply
                        0
                        • R RafagaX

                          Mmmm... you seem determined to read that file, is it important?

                          CEO at: - Rafaga Systems - Para Facturas - Modern Components for the moment...

                          T Offline
                          T Offline
                          Thomas Daniels
                          wrote on last edited by
                          #21

                          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

                          1 Reply Last reply
                          0
                          • Richard DeemingR Richard Deeming

                            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 the finally block is ignored, in which case it shouldn't be allowed, or it replaces any value returned from the try or catch 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

                            B Offline
                            B Offline
                            bkebamc
                            wrote on last edited by
                            #22

                            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 }

                            H B L 3 Replies Last reply
                            0
                            • T Thomas Daniels

                              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

                              J Offline
                              J Offline
                              jnlt
                              wrote on last edited by
                              #23

                              Timed out first time, then worked the second time?

                              1 Reply Last reply
                              0
                              • B Bernhard Hiller

                                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);
                                }
                                }

                                P Offline
                                P Offline
                                PhilLenoir
                                wrote on last edited by
                                #24

                                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.

                                B 1 Reply Last reply
                                0
                                • B Bernhard Hiller

                                  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);
                                  }
                                  }

                                  M Offline
                                  M Offline
                                  Member 7711917
                                  wrote on last edited by
                                  #25

                                  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
                                  }

                                  1 Reply Last reply
                                  0
                                  • B bkebamc

                                    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 }

                                    H Offline
                                    H Offline
                                    H Brydon
                                    wrote on last edited by
                                    #26

                                    :laugh: :-D :) Ha ha that's awesome! A fiver for you!

                                    -- Harvey

                                    1 Reply Last reply
                                    0
                                    • B bkebamc

                                      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 }

                                      B Offline
                                      B Offline
                                      Bernhard Hiller
                                      wrote on last edited by
                                      #27

                                      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?

                                      1 Reply Last reply
                                      0
                                      • B bkebamc

                                        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 }

                                        L Offline
                                        L Offline
                                        lukeer
                                        wrote on last edited by
                                        #28

                                        Your'e doing it wrong! (Even catch blocks end in curly braces.)

                                        Ciao, luker

                                        1 Reply Last reply
                                        0
                                        • Richard DeemingR Richard Deeming

                                          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

                                          K Offline
                                          K Offline
                                          KP Lee
                                          wrote on last edited by
                                          #29

                                          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;
                                              }
                                          
                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups