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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. try - catch - finally

try - catch - finally

Scheduled Pinned Locked Moved ASP.NET
question
8 Posts 4 Posters 1 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.
  • H Offline
    H Offline
    hhrafn
    wrote on last edited by
    #1

    Ok, this I sould know .. however... I'm trying to do something like this: try { myCalss wee = new myClass(); wee.test(); } catch(Exception ee) { Response.Write(ee.ToString()); } finally { wee.Dispose(); } howwever.. wee.Dispose() doesn't work... it doesn't seem to be available at that location .. Where am I going wrong here ? :confused: Thanks alot.

    P J 2 Replies Last reply
    0
    • H hhrafn

      Ok, this I sould know .. however... I'm trying to do something like this: try { myCalss wee = new myClass(); wee.test(); } catch(Exception ee) { Response.Write(ee.ToString()); } finally { wee.Dispose(); } howwever.. wee.Dispose() doesn't work... it doesn't seem to be available at that location .. Where am I going wrong here ? :confused: Thanks alot.

      P Offline
      P Offline
      Plunging_Falcon
      wrote on last edited by
      #2

      why are you even calling the Dispose method? that's what Garbage collectors are for. If you mean by "not available" that you get a "object not set to..." exception then it can be caused by your constructor throwing an exception (not creating the object) and that's why it doesn't exist in the finally block. you need to check: if (wee != null) wee.{method}

      H 1 Reply Last reply
      0
      • P Plunging_Falcon

        why are you even calling the Dispose method? that's what Garbage collectors are for. If you mean by "not available" that you get a "object not set to..." exception then it can be caused by your constructor throwing an exception (not creating the object) and that's why it doesn't exist in the finally block. you need to check: if (wee != null) wee.{method}

        H Offline
        H Offline
        hhrafn
        wrote on last edited by
        #3

        Actually I'm making a class that's handling my sql connections. And the plan was to close the connection on dispose. What I mean with "not available" is that Visual Studio doesn't recognize it in the code editor.

        H 1 Reply Last reply
        0
        • H hhrafn

          Actually I'm making a class that's handling my sql connections. And the plan was to close the connection on dispose. What I mean with "not available" is that Visual Studio doesn't recognize it in the code editor.

          H Offline
          H Offline
          hhrafn
          wrote on last edited by
          #4

          ..Or is the Dispose function called at the end of the run of the page?

          1 Reply Last reply
          0
          • H hhrafn

            Ok, this I sould know .. however... I'm trying to do something like this: try { myCalss wee = new myClass(); wee.test(); } catch(Exception ee) { Response.Write(ee.ToString()); } finally { wee.Dispose(); } howwever.. wee.Dispose() doesn't work... it doesn't seem to be available at that location .. Where am I going wrong here ? :confused: Thanks alot.

            J Offline
            J Offline
            Jeff Martin
            wrote on last edited by
            #5

            wee is out of scope in the finally block. In C#, variables only exist in the scope in which they are declared. Basically whichever set of { } the variable is declared in is the only place you can access it. Plus there is a problem of calling a method on an object in a finally block when you are creating it in the try. What happens if an exception is thrown in the myClass() constructor? You get an unhandled "Object reference not set to an instance of an object" exception. Try this... myClass wee = null; try { wee = new myClass(); wee.test(); } catch (Exception ex) { Response.Write(ex.ToString()); } finally { if (wee != null) wee.Dispose(); } Jeff Martin My Blog

            H 1 Reply Last reply
            0
            • J Jeff Martin

              wee is out of scope in the finally block. In C#, variables only exist in the scope in which they are declared. Basically whichever set of { } the variable is declared in is the only place you can access it. Plus there is a problem of calling a method on an object in a finally block when you are creating it in the try. What happens if an exception is thrown in the myClass() constructor? You get an unhandled "Object reference not set to an instance of an object" exception. Try this... myClass wee = null; try { wee = new myClass(); wee.test(); } catch (Exception ex) { Response.Write(ex.ToString()); } finally { if (wee != null) wee.Dispose(); } Jeff Martin My Blog

              H Offline
              H Offline
              hhrafn
              wrote on last edited by
              #6

              I get a build error: 'myClass' does not contain definition for 'Dispose' My VisualStudio says that myClass only countains a few functions.. btw, Dispose calls the deconstructor automatically, I don't have to define the Dispose() function.. right? thanks.

              J L 2 Replies Last reply
              0
              • H hhrafn

                I get a build error: 'myClass' does not contain definition for 'Dispose' My VisualStudio says that myClass only countains a few functions.. btw, Dispose calls the deconstructor automatically, I don't have to define the Dispose() function.. right? thanks.

                J Offline
                J Offline
                Jeff Martin
                wrote on last edited by
                #7

                No, don't override it unless you need to. I thought you were just using that as an example. Jeff Martin My Blog

                1 Reply Last reply
                0
                • H hhrafn

                  I get a build error: 'myClass' does not contain definition for 'Dispose' My VisualStudio says that myClass only countains a few functions.. btw, Dispose calls the deconstructor automatically, I don't have to define the Dispose() function.. right? thanks.

                  L Offline
                  L Offline
                  Luis Alonso Ramos
                  wrote on last edited by
                  #8

                  AFAIK, to have a Dispose method, you must derive (and implement) from IDisposable. -- LuisR


                  Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!

                  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