Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Try and catch

  1. #1
    Member
    Join Date
    Sep 2012
    Location
    Philippines
    Posts
    46
    My Mood
    Cheeky
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Try and catch

    public void searchBook(int temp)
        {
        	try
            {
                query = "SELECT * FROM ccItems WHERE ISBN = '"+temp+"'";
     
                rs = stmt.executeQuery(query);
                rs.next();
     
                this.setIsbn(rs.getString(1));
                this.setTitle(rs.getString(2));
                this.setAuthor(rs.getString(3));
                this.setSubject(rs.getString(4));
                this.setPublisher(rs.getString(5));
                this.setDate(rs.getString(6));
            }
     
            catch(NumberFormatException nf)
            {
                JOptionPane.showMessageDialog(null, "Numbers Only");
            }
     
            catch(Exception e)
            {
                JOptionPane.showMessageDialog(null, "Error: Could not find Data");
     
                this.setTitle("");
                this.setAuthor("");
                this.setSubject("");
                this.setPublisher("");
                this.setDate("");
            }
        }

    help please my first catch statement wont work.


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Try and catch

    Quote Originally Posted by jaydac12 View Post
    help please my first catch statement wont work.
    "won't work" tells us little, and if you need our help, consider giving us more details on your problem. The better the information you can give us, usually the better the help we can give back.

  3. #3
    Member Zyrion's Avatar
    Join Date
    Feb 2013
    Location
    Iowa
    Posts
    106
    My Mood
    Angelic
    Thanks
    2
    Thanked 8 Times in 8 Posts

    Default Re: Try and catch

    It seems you are trying to use multiple-exception catches. What problems are you having exactly? What errors is your compiler throwing at you, if any?

    try {
         // stuff
    } catch (Exception1 | Exception2 ex) {
         // Handle both exceptions
    }

    try {
         // stuff
    } catch (SuperException ex) {
         if (ex instanceof Exception1 || ex instanceof Exception2) {
             // handle exception
         }
    }

Similar Threads

  1. how do i try/catch this?
    By safra in forum Exceptions
    Replies: 6
    Last Post: October 29th, 2011, 11:14 AM
  2. Always Catch Exceptions
    By Tjstretch in forum Java Programming Tutorials
    Replies: 2
    Last Post: October 27th, 2011, 02:26 PM
  3. catch all
    By silverspoon34 in forum Exceptions
    Replies: 1
    Last Post: November 29th, 2009, 02:18 PM
  4. try/catch
    By rsala004 in forum Exceptions
    Replies: 5
    Last Post: July 19th, 2009, 03:20 PM