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. General Programming
  3. C#
  4. a try inside another

a try inside another

Scheduled Pinned Locked Moved C#
help
38 Posts 15 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.
  • J J4amieC

    ahhh, no no no. If your UI code is mixed with your database access code; you're doing it wrong If you show Exception messages unsanitized to your users; you're doing it wrong

    G Offline
    G Offline
    Gary Wheeler
    wrote on last edited by
    #27

    J4amieC wrote:

    If your UI code is mixed with your database access code; you're doing it wrong
     
    If you show Exception messages unsanitized to your users; you're doing it wrong

    Ahhh, no. It depends upon the scope of the problem you're trying to solve. If this is a 1,000 line utility app that you're the only user for, this might be perfectly appropriate. If it's a 200,000 line client for a LOB app, then you might have issues.

    Software Zen: delete this;

    1 Reply Last reply
    0
    • J Jan Holst Jensen2

      Pointless with regards to how the code executes, but not pointless if you are debugging. The outer catch block gives you a place to put a breakpoint so you can see when exceptions occur. I expect that is the reason for it. But the outer try-catch block can be safely removed for production code.

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

      Then put a catch on the try/finally.

      J 1 Reply Last reply
      0
      • P PIEBALDconsult

        Then put a catch on the try/finally.

        J Offline
        J Offline
        Jan Holst Jensen2
        wrote on last edited by
        #29

        Except if the exception occurs during execution of the finally-block...

        1 Reply Last reply
        0
        • R Rick van Woudenberg

          Well, as far as I'm concerned both would be nice. Sure, the connection to the database has priority over user notification, however when something stuffs up, I generally let the user know. In that particular case I would do something like :

          private void DoSomething()
          {
          SqlConnection connection = null;
          try
          {
          connection = new SqlConnection();
          // Do something that might cause an exception...
          connection.Open();
          }
          catch(SqlException ex)
          {
          MessageBox.Show(ex.ToString(); // or something else to notify the customer
          }
          finally
          {
          if (connection.ConnectionState == ConnectionState.Open)
          connection.Close();
          }
          }

          Then you actually have both of two worlds. However, that puts us right back to the essence of this discussion. Having a catch clause in a method is not something to be ashamed of, though I get the feeling that many developers think that way.

          D Offline
          D Offline
          DragonLord66
          wrote on last edited by
          #30

          If you're in a helper layer such as a DAL then you don't want to add any code that will interact with the user, however you may want to do clean up before the exception is thrown up the chain to a level where the exception can be dealt with by the user. Equally what's the difference between the original example and the following?

          Private Sub ExceptionMethod()
              Try
                  DoExceptionCode()
              Finally
                  DoCleanup()
              End Try
          End Sub
          
          Private Sub ExceptionHandlerCode()
              Try
                  ExceptionMethod()
              Catch ex As Exception
                  ExceptionHandlerHere()
              End Try
          End Sub
          
          P 1 Reply Last reply
          0
          • D DragonLord66

            If you're in a helper layer such as a DAL then you don't want to add any code that will interact with the user, however you may want to do clean up before the exception is thrown up the chain to a level where the exception can be dealt with by the user. Equally what's the difference between the original example and the following?

            Private Sub ExceptionMethod()
                Try
                    DoExceptionCode()
                Finally
                    DoCleanup()
                End Try
            End Sub
            
            Private Sub ExceptionHandlerCode()
                Try
                    ExceptionMethod()
                Catch ex As Exception
                    ExceptionHandlerHere()
                End Try
            End Sub
            
            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #31

            Arrgggh - my eyes. The horror. Case insensitive code in our lovely curly bracketed case sensitive world.

            Forgive your enemies - it messes with their heads

            My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

            D J 2 Replies Last reply
            0
            • P Pete OHanlon

              Arrgggh - my eyes. The horror. Case insensitive code in our lovely curly bracketed case sensitive world.

              Forgive your enemies - it messes with their heads

              My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

              D Offline
              D Offline
              DragonLord66
              wrote on last edited by
              #32

              Sorry, I'll use Smalltalk next time. More seriously I had a vb editor open so it was just quicker to type it in there with the auto complete than to start up a new c# editor for the example (formatting purposes)

              P 1 Reply Last reply
              0
              • D DragonLord66

                Sorry, I'll use Smalltalk next time. More seriously I had a vb editor open so it was just quicker to type it in there with the auto complete than to start up a new c# editor for the example (formatting purposes)

                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #33

                DragonLord66 wrote:

                I had a vb editor open

                In the name of all that's holy man, why?

                Forgive your enemies - it messes with their heads

                My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                D 1 Reply Last reply
                0
                • P Pete OHanlon

                  DragonLord66 wrote:

                  I had a vb editor open

                  In the name of all that's holy man, why?

                  Forgive your enemies - it messes with their heads

                  My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                  D Offline
                  D Offline
                  DragonLord66
                  wrote on last edited by
                  #34

                  We have a very large code base of VB code that was written simply because it's easier to get a working prototype in vb (pre 2010 and c# runtime code editing), and the prototypes turned into production code...

                  1 Reply Last reply
                  0
                  • A Ali Al Omairi Abu AlHassan

                    guys; I was exminning some code and i found this:

                    try
                    {
                        try
                        {
                            ...
                        }
                        finally
                        {
                            ...
                        }
                    }
                    catch
                    {
                        throw;
                    }
                    

                    I am wondering if this is legal. I mean catch anything and trow anything; or maybe it's usefull for something. because the developer who write this code is someone i believe he is an expert. Thank you;

                    Help people,so poeple can help you.

                    P Offline
                    P Offline
                    patbob
                    wrote on last edited by
                    #35

                    My memory may not be correct, but I seem to recall a time in C++ when try-finally was a macro and try-catch a language intrinsic, so you couldn't mix them together. If you wanted to do both, you pretty much had to code it up that way. Maybe I recall wrong though? Unless there's more code inside the try-catch that isn't inside the try-finally, it doesn't make much sense to code it that way.

                    patbob

                    1 Reply Last reply
                    0
                    • P Pete OHanlon

                      Arrgggh - my eyes. The horror. Case insensitive code in our lovely curly bracketed case sensitive world.

                      Forgive your enemies - it messes with their heads

                      My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                      J Offline
                      J Offline
                      James Lonero
                      wrote on last edited by
                      #36

                      Oh come on, its just another way of saying (explaining) the same thing.

                      1 Reply Last reply
                      0
                      • A Ali Al Omairi Abu AlHassan

                        guys; I was exminning some code and i found this:

                        try
                        {
                            try
                            {
                                ...
                            }
                            finally
                            {
                                ...
                            }
                        }
                        catch
                        {
                            throw;
                        }
                        

                        I am wondering if this is legal. I mean catch anything and trow anything; or maybe it's usefull for something. because the developer who write this code is someone i believe he is an expert. Thank you;

                        Help people,so poeple can help you.

                        S Offline
                        S Offline
                        SilimSayo
                        wrote on last edited by
                        #37

                        It is legal but personally, I prefer

                        try
                        {

                        ////whole batch of statements that could give raise to an exception

                        }
                        //Several catch blocks
                        catch (IOException e)
                        {

                        }
                        catch(MyException e)
                        {

                        }
                        .
                        .
                        .
                        .
                        .
                        .
                        catch (Exception e)///Generic catch block
                        {

                        }

                        1 Reply Last reply
                        0
                        • A Ali Al Omairi Abu AlHassan

                          guys; I was exminning some code and i found this:

                          try
                          {
                              try
                              {
                                  ...
                              }
                              finally
                              {
                                  ...
                              }
                          }
                          catch
                          {
                              throw;
                          }
                          

                          I am wondering if this is legal. I mean catch anything and trow anything; or maybe it's usefull for something. because the developer who write this code is someone i believe he is an expert. Thank you;

                          Help people,so poeple can help you.

                          S Offline
                          S Offline
                          Sandy_L_Schultz
                          wrote on last edited by
                          #38

                          That's a do-while. :laugh:

                          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