try catch blocks

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike P

    #1

    try catch blocks

    I'm trying to split up a large procedure into several try catch blocks
    in order to catch different errors. However, when I split my code up
    like this, my variables that I am using throughout the procedure are now
    only recoginsed within their own try catch block instead of the whole
    procedure.

    How do you get around this? By making all your variables public?


    Any help would be much appreciated.

    Cheers,

    Mike



    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
  • Jon Skeet

    #2
    Re: try catch blocks

    Mike P <mrp@telcoelect ronics.co.uk> wrote:[color=blue]
    > I'm trying to split up a large procedure into several try catch blocks
    > in order to catch different errors. However, when I split my code up
    > like this, my variables that I am using throughout the procedure are now
    > only recoginsed within their own try catch block instead of the whole
    > procedure.
    >
    > How do you get around this? By making all your variables public?[/color]

    Declare the variables outside the try block, instead of inside.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Mike P

      #3
      Re: try catch blocks

      I realised I should declare my variables outside the block about 5
      seconds after I posted...sorry!

      However, if I want to put a try catch block within a switch statement do
      I do this exactly the same way as normal? Or do I put a return before
      the break to exit the procedure if I catch an error?


      Mike



      *** Sent via Developersdex http://www.developersdex.com ***
      Don't just participate in USENET...get rewarded for it!

      Comment

      • Eliyahu Goldin

        #4
        Re: try catch blocks

        > However, if I want to put a try catch block within a switch statement do[color=blue]
        > I do this exactly the same way as normal?[/color]
        yes
        [color=blue]
        > Or do I put a return before
        > the break to exit the procedure if I catch an error?[/color]
        This question is not specific to switch statement. Everything depends on
        your program logic and what you want to do on errors.

        Eliyahu


        Comment

        • Sander Pilon

          #5
          Re: try catch blocks


          "Mike P" <mrp@telcoelect ronics.co.uk> wrote in message
          news:Ori%23Nn3T DHA.2116@TK2MSF TNGP12.phx.gbl. ..[color=blue]
          > I realised I should declare my variables outside the block about 5
          > seconds after I posted...sorry!
          >
          > However, if I want to put a try catch block within a switch statement do
          > I do this exactly the same way as normal? Or do I put a return before
          > the break to exit the procedure if I catch an error?
          >
          >
          > Mike[/color]

          You can also just throw different exceptions in a single try-catch block,
          and figure out the different errors from the type of exception.


          Comment

          • james

            #6
            Re: try catch blocks

            I think it is important to note that try catch blocks
            are no different than any other block as far as scoping
            of variables go. Any time you use { } you have created
            a new scope. Variables declared inside of { } will not be seen
            outside that scope.

            JIM


            "Mike P" <mrp@telcoelect ronics.co.uk> wrote in message
            news:Ori#Nn3TDH A.2116@TK2MSFTN GP12.phx.gbl...[color=blue]
            > I realised I should declare my variables outside the block about 5
            > seconds after I posted...sorry!
            >
            > However, if I want to put a try catch block within a switch statement do
            > I do this exactly the same way as normal? Or do I put a return before
            > the break to exit the procedure if I catch an error?
            >
            >
            > Mike
            >
            >
            >
            > *** Sent via Developersdex http://www.developersdex.com ***
            > Don't just participate in USENET...get rewarded for it![/color]


            Comment

            Working...