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 25 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 Offline
    T Offline
    Thomas Daniels
    wrote on last edited by
    #1

    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 M B P J 8 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

      R Offline
      R Offline
      RugbyLeague
      wrote on last edited by
      #2

      If at first you don't succeed...

      T 1 Reply Last reply
      0
      • R RugbyLeague

        If at first you don't succeed...

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

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

        ProgramFOX

        _ 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

          M Offline
          M Offline
          Mark_Wallace
          wrote on last edited by
          #4

          Makes sense to me. If you're considering stopping reading your e-book and getting some work done, it deals with your crisis of conscience for you.

          I wanna be a eunuchs developer! Pass me a bread knife!

          T 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

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

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

            Richard DeemingR G P M K 5 Replies 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);
              }
              }

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

              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

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

              B A K 3 Replies 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

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

                Yes, you are right. So let's remove the last "return" (but not the File.ReadAllText).

                1 Reply Last reply
                0
                • M Mark_Wallace

                  Makes sense to me. If you're considering stopping reading your e-book and getting some work done, it deals with your crisis of conscience for you.

                  I wanna be a eunuchs developer! Pass me a bread knife!

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

                  Mark Wallace wrote:

                  If you're considering stopping reading your e-book

                  But... How can I stop reading my e-book if I'm not reading a e-book?

                  ProgramFOX

                  B 1 Reply Last reply
                  0
                  • T Thomas Daniels

                    Mark Wallace wrote:

                    If you're considering stopping reading your e-book

                    But... How can I stop reading my e-book if I'm not reading a e-book?

                    ProgramFOX

                    B Offline
                    B Offline
                    Brisingr Aerowing
                    wrote on last edited by
                    #9

                    You don't. You throw a IAmNotReadingAnEBookException!

                    I think computer viruses should count as life. I think it says something about human nature that the only form of life we have created so far is purely destructive. We've created life in our own image. Stephen Hawking

                    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

                      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
                                          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