Results 1 to 16 of 16

Thread: Jumping to a finally {}

  1. #1

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729

    Jumping to a finally {}

    i have a following try catch block

    try {
    if () {}
    else { NOW i wanna go directly to a finally{} block
    }

    is there a way to do that?
    \m/\m/

  2. #2
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Well you can use goto, but its frowned upon.
    Dont gain the world and lose your soul

  3. #3

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    but how? goto finally

    ? or what?
    \m/\m/

  4. #4
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    I haven't tried it, but try the break statement:

    break;

    It is useful in loops when you want out, maybe you can break out of a try block...

  5. #5

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    no, i dont think so because its just for loops and switches
    \m/\m/

  6. #6
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Nevermind, just tried it, it only works for loops.

  7. #7

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    i cant make goto to a finally block?
    \m/\m/

  8. #8
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    After thinking about what your trying to do, I think you are probably approaching the problem wrong. You shouldn't be using error trapping as flow control for your program. You should use it to trap and handle errors. Could you maybe post the whole code your trying to do so I can see if there is a different way for you to go about it.

    Jumping to different blocks of code in the same method isn't good practice anyway.

  9. #9
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Looking at your example, you can just leave out the else part of the if statement and if the statement isn't true, the finally block will execute:
    Code:
    try
    {
       if()
       {
           // your code here
        }
    }
    catch
    {
    }
    finally
    {
      //Your finally code here.
    }

  10. #10

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    i had the idea of throwing my own exception
    \m/\m/

  11. #11
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    No, that's bad.

    But your problem is irrelevant IMO. It seems you want to do something like
    Code:
    try {
      // stuff
      if(expr) {
        // stuff
      } else {
        // stuff
      }
      // stuff you don't want if it's else
    } finally {
      // stuff
    }
    In that case it should be
    Code:
    try {
      // stuff
      if(expr) {
        // stuff
        // stuff you don't want if it's else
      } else {
        // stuff
      }
    } finally {
      // stuff
    }
    The finally blocks executes even if there is no error.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  12. #12
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Use the return keyword to exit the routine. B/c you are using a finally statement, it will always fire:

    Code:
    private void button1_Click(object sender, System.EventArgs e)
    {
        bool flag = false;
        try
        {
            if (flag)
    	{
    	    Console.WriteLine("Do Something..");
    	}
    	else
    	{
    	    return;
    	}
        }
        finally
        {
            MessageBox.Show("Finally Block Executing..");
        }
    }

  13. #13

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    always? didnt know of that..iamgine the following example:

    Code:
    try {
    	if (a == 5) {
    		return true;
    	else {
    		MessageBox.show("error");
    	}
    }
    finally {
    	return false;
    	}
    }
    this means that it will return false if the value is equal to 5?
    \m/\m/

  14. #14
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    You can't use the return keyword in the finally() clause.

  15. #15

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    hmm interesting fact
    \m/\m/

  16. #16
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    But if you were using the Windows SEH in C or C++ then it would always return false, no matter what the value of a is.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width