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 4 of 4

Thread: Where would I place try and catch in this code?

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Where would I place try and catch in this code?

    import java.applet.Applet;
    import java. awt.*;
     
    public class RandomActsofPictures extends Applet {
    Image action;
     
    public void start(){
    int rint = (int)(Math.random() * 3);
    System.out.println("var=" + rint);
    if(rint == 0){
    	System.out.println("var=" + rint);
        action = getImage(getDocumentBase(), "../Images/1bee.jpg");
     
    } else if(rint == 1){
    	System.out.println("var=" + rint);
        action = getImage(getDocumentBase(), "../Images/2bees.jpg");
    } else {
    	System.out.println("var=" + rint);
        action = getImage(getDocumentBase(), "../Images/3bees.jpg");
    }
    }
        public void paint(Graphics graph) {
     
         graph.drawImage(action, 10, 50, this);
         resize(300,300);
        }
        }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Where would I place try and catch in this code?

    What methods throw an exception?
    What do you want to do if one of them throws an exception?
    For example if a method throws an exception you might want to provide a default value
    or end the program.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Where would I place try and catch in this code?

    I put try; right before the if statements and I get an error saying I need to put finally after the last closing bracket. I'm familiar with catch(Exceptions e) but not finally can someone explain why you need finally?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Where would I place try and catch in this code?

    When you get errors, please copy the full text and paste it along with the source that caused it.

    See the tutorial: The Catch or Specify Requirement (The Java™ Tutorials > Essential Classes > Exceptions)
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. A Pixel Out Of Place
    By tyeeeee1 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 4th, 2012, 07:06 PM
  2. One Decimal Place
    By Akirien in forum Object Oriented Programming
    Replies: 5
    Last Post: August 22nd, 2012, 12:11 AM
  3. How to think about LOOPS in the first place
    By searchformeaning in forum Object Oriented Programming
    Replies: 6
    Last Post: May 1st, 2012, 01:05 PM
  4. Where to place my own library?
    By hexwind in forum Java Theory & Questions
    Replies: 3
    Last Post: June 22nd, 2011, 06:25 AM