Results 1 to 7 of 7
  1. #1
    GuteFco is offline Member
    Join Date
    Feb 2014
    Posts
    3
    Rep Power
    0

    Default Multiple catch bug?

    I have the next code:

    MyException:
    Java Code:
    public class MyException extends Exception {  
        public MyException(String mensaje){
            super(mensaje);
        }
    }
    next in some place i have:

    In some place:
    Java Code:
    try{
        f.setStock(Integer.parseInt(tflStock.getText()));
                    
        f.save();
    } catch (MyException e) {
        System.err.println("My Exceptioon");
    } catch(SQLException | ClassNotFoundException e){
        System.err.println("DB Exception");
    }
    The NumberFormatException is not in any catch sentence but if parseInt throw the NumberFormatException the SQLException | ClassNotFoundException catch block is running (I can't figure why), if I rewrite the code:

    Rewriten code:
    Java Code:
    try{
        f.setStock(Integer.parseInt(tflStock.getText()));
                    
        f.save();
    } catch (MyException e) {
        System.err.println("My Exceptioon");
    } catch(SQLException e){
        System.err.println("DB Exception");
    } catch (ClassNotFoundException e){
        System.err.println("DB Exception");
    }
    Then none of the catch block is excecution (this is why I expect).

    Anyone know why is happening?

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: Multiple catch bug?

    I interpret differently: in your first example the parseInt() is not causing an exception, so the code goes on to the f.save(), blows up and throws an SQLException. In stead of hiding the information, add printStackTrace() calls to your exception handling blocks and see the actual exception that is being caused and by which statement.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    GuteFco is offline Member
    Join Date
    Feb 2014
    Posts
    3
    Rep Power
    0

    Default Re: Multiple catch bug?

    Quote Originally Posted by gimbal2 View Post
    I interpret differently: in your first example the parseInt() is not causing an exception, so the code goes on to the f.save(), blows up and throws an SQLException. In stead of hiding the information, add printStackTrace() calls to your exception handling blocks and see the actual exception that is being caused and by which statement.
    I get the stacktrace it says:
    Java Code:
    java.lang.NumberFormatException: For input string: ""
    	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    	at java.lang.Integer.parseInt(Integer.java:592)
    	at java.lang.Integer.parseInt(Integer.java:615)
    	at cl.pollero.controladores.CrearFamiliaController.lambda$configurarBotonCrear$1(CrearFamiliaController.java:92)
            ...
    the line with the:

    Java Code:
    f.setStock(Integer.parseInt(tflStock.getText()));
    any way, if you ask me the two snipped code (multiple catch and one catch at time) are the same, but they work differently, I'm using Java 8, may be a bug?

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: Multiple catch bug?

    I can't say, I'm not a developer on the Java 8 development team and I am not going to consider using it before it has been officially released and had at least two patch releases. If you can produce a small test application which shows the problem, I'll be happy to take a look running it through Java 7.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    GuteFco is offline Member
    Join Date
    Feb 2014
    Posts
    3
    Rep Power
    0

    Default Re: Multiple catch bug?

    I fix the problem, the try catch is right, the problem was in the outter code (an EventHandler), example:

    Working well:
    Java Code:
          btnCrear.setOnAction(new EventHandler<ActionEvent>() {
                public void handle(ActionEvent t) {
                    try{
                        Integer.parseInt("a");
                        
                        if(false) {
                            throw new SQLException("never");
                        }
                        
                        if(false) {
                            throw new ClassNotFoundException("never");
                        }
                        
                        if(false) {
                            throw new MyException("never");
                        }
                    } catch(SQLException | ClassNotFoundException e){
                        System.err.println("Here 1");
                    } catch (ValidaException e) {
                        System.err.println("Here 2");
                    }
                }
            });
    Not well:
    Java Code:
          btnCrear.setOnAction((ActionEvent t) -> {  
              try {
                    Integer.parseInt("a");
                    
                    if(false) {
                        throw new SQLException("never");
                    }
                    
                    if(false) {
                        throw new ClassNotFoundException("never");
                    }
                    
                    if(false) {
                        throw new ValidaException("never");
                    }
                } catch(SQLException | ClassNotFoundException e){
                    System.err.println("Here 1");
                } catch (ValidaException e) {
                    System.err.println("Here 2");
                }
            });
    In the first case all works like I excepted, but in the second way (using Lambda expression suggestesd by Netbeans) the NumberFormatException is catch in the SQLException | ClassNotFoundException e

    Thanks for all

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: Multiple catch bug?

    Okay if you say so, I still wouldn't know the why at this point.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Multiple catch bug?

    Are you sure it's not a SQLException caused by a NumberFormatException?
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. Multiple exception catch block
    By niloufar in forum New To Java
    Replies: 5
    Last Post: 10-18-2012, 01:11 AM
  2. Multiple try & catch statements within a switch
    By Martyn in forum New To Java
    Replies: 21
    Last Post: 01-12-2012, 10:24 PM
  3. One process open multiple threads on multiple CPUs
    By kfcnhl in forum Threads and Synchronization
    Replies: 2
    Last Post: 12-12-2011, 09:27 AM
  4. [SOLVED] multiple try catch
    By AndrewM16921 in forum New To Java
    Replies: 3
    Last Post: 04-01-2009, 10:12 PM
  5. how to catch two exceptions in one catch()?
    By arnab321 in forum New To Java
    Replies: 1
    Last Post: 11-06-2008, 11:54 AM

Posting Permissions

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