Results 1 to 14 of 14

Thread: breaking out of a method [resolved]

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    breaking out of a method [resolved]

    Does anyone if there is a way to break out of a method in Java?
    I have a return statement inside of a catch block and when an exception is thrown the contents of the catch block are executed but instead of the method returning back to it's caller the code after the try{ is being executed. Is there another way to handle this?
    Last edited by Dilenger4; Oct 1st, 2001 at 03:10 PM.

  2. #2
    You mean this doesn't work?

    Code:
    public void crashMeBaby()
    {
        try
        {
            // 1
            super.badStuffThatCausesExceptions();
            // 2
        }
        catch (Exception e)
        {
            // 3
            return;
            // 4
        }
    }
    Which comment does it get to before nuking itself?

  3. #3

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    I have somthing like this...... whats happining is somthing like
    Visual Basics On Error ResumeNext.

    Code:
      public static void X(JTextField jtf){
         String filepath = jtf.getText(); 
    
          try{
          FileReader fr = new FileReader(filepath); 
    
         /* {
                this is the code i dont want executed
         */ }
    
    
    
         }catch(Exception e){
          JOptionPane.showMessageDialog(GUI, " Unable to find file      requested " + " Please try again ");
          jtf.setText("");
          return;  // should return back to it's caller
        }
     }

  4. #4
    Code:
    public static void X(JTextField jtf){
         String filepath = jtf.getText(); 
    
          try{
          FileReader fr = new FileReader(filepath); 
    
          System.out.println("WOOHOO!");
    
    
    
         }catch(Exception e){
          JOptionPane.showMessageDialog(GUI, " Unable to find file      requested " + " Please try again ");
          jtf.setText("");
          return;  // should return back to it's caller
        }
     }
    So in that example, the message isn't printed? That's completely screwed up. What JDK do you have?

  5. #5

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    The MessageDialog pops up fine and the JTextField is cleared
    but instead of the method just returning the System.out.print
    that you have would print out "WOOHOO!"

    Code:
    public static void X(JTextField jtf){
         String filepath = jtf.getText(); 
    
          try{
          FileReader fr = new FileReader(filepath); 
    
          System.out.println("WOOHOO!");
    
    
    
         }catch(Exception e){
          JOptionPane.showMessageDialog(GUI, " Unable to find file      requested " + " Please try again ");
          jtf.setText("");
          return;  // should return back to it's caller
        }
     }

  6. #6
    Originally posted by filburt1
    What JDK do you have?

  7. #7

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    1.3

  8. #8

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    I always compile from the bin, so i have to goto cd C:\jdk1.3\bin

  9. #9

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    I just dont see how i could be somthing that i am coding wrong.
    If you have a return statement anywhere in a method it should return back to it's caller. It's like when a method throws an Exception ie..... public static void computeSquareRoot(int root) throws Exception{}
    the Exception would propagate back to it's caller.

  10. #10
    I know.

    Unless you forgot braces around the new keyword, I have no idea.

  11. #11

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    I posted this on Sun's site with a couple of Duke Dollars
    on it so i should have a couple of responses.

  12. #12
    Originally posted by Dilenger4
    I posted this on Sun's site with a couple of Duke Dollars
    WOOHOO! PROGRAMMERS WORKING FOR SUN MEMORIBILIA!

  13. #13

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    I was missing a Bracket. Dam Brackets!

  14. #14
    Originally posted by filburt1
    Unless you forgot braces around the try keyword, I have no idea.
    BOOYA!

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