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. Throwing exception

Throwing exception

Scheduled Pinned Locked Moved The Weird and The Wonderful
10 Posts 6 Posters 0 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.
  • M Offline
    M Offline
    Mohibur Rashid
    wrote on last edited by
    #1

    I was reviewing source code. I found this piece

    try {
    if(!$var) {
    // just this line;
    throw new Exception("Exception related message");
    }
    // some other code
    } catch(Exception e) {
    // no code for handling the exception
    // do some other irrelevant task
    }

    :wtf: :wtf: :wtf: :wtf: :wtf: :wtf: :wtf: :wtf:

    I do not fear of failure. I fear of giving up out of frustration.

    S P N 3 Replies Last reply
    0
    • M Mohibur Rashid

      I was reviewing source code. I found this piece

      try {
      if(!$var) {
      // just this line;
      throw new Exception("Exception related message");
      }
      // some other code
      } catch(Exception e) {
      // no code for handling the exception
      // do some other irrelevant task
      }

      :wtf: :wtf: :wtf: :wtf: :wtf: :wtf: :wtf: :wtf:

      I do not fear of failure. I fear of giving up out of frustration.

      S Offline
      S Offline
      Super Lloyd
      wrote on last edited by
      #2

      Hahahaha... And it's the first time you encounter such code hey? Welcome to the club! :laugh: It seems to me that most code written by junior has such silly "exception handling"...

      A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

      M 1 Reply Last reply
      0
      • S Super Lloyd

        Hahahaha... And it's the first time you encounter such code hey? Welcome to the club! :laugh: It seems to me that most code written by junior has such silly "exception handling"...

        A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

        M Offline
        M Offline
        Mohibur Rashid
        wrote on last edited by
        #3

        i have dealt with many madness in engineering. But this ticked me off. I cordially asked the engineer to fix. But he does not see the issue. but One good thing is he agreed to change the way i think it is okay

        I do not fear of failure. I fear of giving up out of frustration.

        S 1 Reply Last reply
        0
        • M Mohibur Rashid

          i have dealt with many madness in engineering. But this ticked me off. I cordially asked the engineer to fix. But he does not see the issue. but One good thing is he agreed to change the way i think it is okay

          I do not fear of failure. I fear of giving up out of frustration.

          S Offline
          S Offline
          Super Lloyd
          wrote on last edited by
          #4

          maybe he doesn't know about "return" statement or "finally" clause?

          A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

          Sander RosselS 1 Reply Last reply
          0
          • M Mohibur Rashid

            I was reviewing source code. I found this piece

            try {
            if(!$var) {
            // just this line;
            throw new Exception("Exception related message");
            }
            // some other code
            } catch(Exception e) {
            // no code for handling the exception
            // do some other irrelevant task
            }

            :wtf: :wtf: :wtf: :wtf: :wtf: :wtf: :wtf: :wtf:

            I do not fear of failure. I fear of giving up out of frustration.

            P Offline
            P Offline
            Philippe Mori
            wrote on last edited by
            #5

            Well, at first it seems that a simple if statement would do… However, if the condition should not occurs, it might be easier to find the problem during debugging if the debugger stop on exception or write something to the output log. But let assume that some other code might also throw an Exception and if that case, you want to execute the code in catch clause too, then it might be acceptable after all if that code should not be executed if no exception are thrown. As shown in the above example, all code can be removed as nothing else is done. In real code, it would not be the case! And the **cleanest** code to write directly depends on what missing code do and if some other code could throw or not.

            Philippe Mori

            M 1 Reply Last reply
            0
            • S Super Lloyd

              maybe he doesn't know about "return" statement or "finally" clause?

              A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

              Sander RosselS Offline
              Sander RosselS Offline
              Sander Rossel
              wrote on last edited by
              #6

              But do you know about the "fault" clause? It's like "finally", but only executes when an Exception was thrown. It's basically like:

              catch
              {
              // Do something.
              throw;
              }

              But faster since the Exception isn't actually caught and rethrown. I guess it would look like:

              fault
              {
              // Do something.
              }

              The compiled MSIL code supports it, C# does not (which doesn't mean you can't use it[^] :cool:) :D

              Best, Sander Continuous Integration, Delivery, and Deployment arrgh.js - Bringing LINQ to JavaScript Object-Oriented Programming in C# Succinctly

              S 1 Reply Last reply
              0
              • P Philippe Mori

                Well, at first it seems that a simple if statement would do… However, if the condition should not occurs, it might be easier to find the problem during debugging if the debugger stop on exception or write something to the output log. But let assume that some other code might also throw an Exception and if that case, you want to execute the code in catch clause too, then it might be acceptable after all if that code should not be executed if no exception are thrown. As shown in the above example, all code can be removed as nothing else is done. In real code, it would not be the case! And the **cleanest** code to write directly depends on what missing code do and if some other code could throw or not.

                Philippe Mori

                M Offline
                M Offline
                Mohibur Rashid
                wrote on last edited by
                #7

                You have an excellent point. But this is not the case. I would have understand the point very much if there were other codes that might have thrown exception. There is none :) that's why the frustration.

                I do not fear of failure. I fear of giving up out of frustration.

                1 Reply Last reply
                0
                • Sander RosselS Sander Rossel

                  But do you know about the "fault" clause? It's like "finally", but only executes when an Exception was thrown. It's basically like:

                  catch
                  {
                  // Do something.
                  throw;
                  }

                  But faster since the Exception isn't actually caught and rethrown. I guess it would look like:

                  fault
                  {
                  // Do something.
                  }

                  The compiled MSIL code supports it, C# does not (which doesn't mean you can't use it[^] :cool:) :D

                  Best, Sander Continuous Integration, Delivery, and Deployment arrgh.js - Bringing LINQ to JavaScript Object-Oriented Programming in C# Succinctly

                  S Offline
                  S Offline
                  Super Lloyd
                  wrote on last edited by
                  #8

                  wow, you got me there pal! :omg:

                  A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                  1 Reply Last reply
                  0
                  • M Mohibur Rashid

                    I was reviewing source code. I found this piece

                    try {
                    if(!$var) {
                    // just this line;
                    throw new Exception("Exception related message");
                    }
                    // some other code
                    } catch(Exception e) {
                    // no code for handling the exception
                    // do some other irrelevant task
                    }

                    :wtf: :wtf: :wtf: :wtf: :wtf: :wtf: :wtf: :wtf:

                    I do not fear of failure. I fear of giving up out of frustration.

                    N Offline
                    N Offline
                    Nathan Minier
                    wrote on last edited by
                    #9

                    I wish this were weird, but one of the OOAD commandments I was taught was: "Thou shalt not use exceptions for flow control." Clearly all too common.

                    "Never attribute to malice that which can be explained by stupidity." - Hanlon's Razor

                    R 1 Reply Last reply
                    0
                    • N Nathan Minier

                      I wish this were weird, but one of the OOAD commandments I was taught was: "Thou shalt not use exceptions for flow control." Clearly all too common.

                      "Never attribute to malice that which can be explained by stupidity." - Hanlon's Razor

                      R Offline
                      R Offline
                      raddevus
                      wrote on last edited by
                      #10

                      OOAD commandments:

                      "Thou shalt not use exceptions for flow control."

                      Right. Because exceptions used for flow control are gotos. :) The code jumps to the exception handling routine on error, just as a goto jumps to another place.

                      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