Results 1 to 6 of 6
  1. #1
    arc_remiel is offline Member
    Join Date
    Jan 2011
    Posts
    9
    Rep Power
    0

    Default unreported exception java.lang.Exception; must be caught or declared to be thrown

    help guys.. i alway get this.. i'm new to java and exception handling

    and been following a guide

    but this error shows..


    heres part of my code:

    public class singleTwo extends Canvas implements CommandListener {
    /**
    * constructor
    */
    Random rnd = new Random();
    String who;
    int totalP, totalC, score, stage, ctr;
    int PlayerTemp1, PlayerTemp2, PlayerTemp3,OppTemp1, OppTemp2, OppTemp3;
    int playerVal1, playerVal2, playerVal3, oppVal1, oppVal2,oppVal3;
    Image playerFirst, playerSecond, playerDraw, oppFirst, oppSecond, oppDraw = null;
    Graphics g;


    Image c1 = Image.createImage("/images/club_A.png");
    Image c2 = Image.createImage("/images/club_2.png");
    Image c3 = Image.createImage("/images/club_3.png");
    Image c4 = Image.createImage("/images/club_4.png");
    Image c5 = Image.createImage("/images/club_5.png");
    Image c6 = Image.createImage("/images/club_6.png");
    Image c7 = Image.createImage("/images/club_7.png");
    Image c8 = Image.createImage("/images/club_8.png");
    Image c9 = Image.createImage("/images/club_9.png");
    Image c10 = Image.createImage("/images/club_10.png");
    Image cj = Image.createImage("/images/club_j.png");
    Image cq = Image.createImage("/images/club_q.png");
    Image ck = Image.createImage("/images/club_k.png");

    Image d1 = Image.createImage("/images/diamond_A.png");
    Image d2 = Image.createImage("/images/diamond_2.png");
    Image d3 = Image.createImage("/images/diamond_3.png");
    Image d4 = Image.createImage("/images/diamond_4.png");
    Image d5 = Image.createImage("/images/diamond_5.png");
    Image d6 = Image.createImage("/images/diamond_6.png");
    Image d7 = Image.createImage("/images/diamond_7.png");
    Image d8 = Image.createImage("/images/diamond_8.png");
    Image d9 = Image.createImage("/images/diamond_9.png");
    Image d10 = Image.createImage("/images/diamond_10.png");
    Image dj = Image.createImage("/images/diamond_j.png");
    Image dq = Image.createImage("/images/diamond_q.png");
    Image dk = Image.createImage("/images/diamond_k.png");







    public void singleTwo() throws Exception {
    setFullScreenMode(true);

    try {
    // Set up this canvas to listen to command events
    setCommandListener(this);
    // Add the Exit command
    addCommand(new Command("Exit", Command.EXIT, 1));





    } catch(Exception e) {
    e.printStackTrace();
    }

    }

    i don't get it..the error disappeared after I tried to put it inside try{} but then the variables of the image needed for a code outside of try{} is then hidden..

    thanks guys

  2. #2
    arc_remiel is offline Member
    Join Date
    Jan 2011
    Posts
    9
    Rep Power
    0

    Default

    oh by the way..the error is pertaining to the images having unreported IOException..

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default

    It's pretty self-explanatory: the line that causes the error can throw an IOException that you aren't catching, and the method that contains that line isn't declared to throw it. Do one of those things.

    As for your problem with variable scope, you can declare the variables outside of the try.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    arc_remiel is offline Member
    Join Date
    Jan 2011
    Posts
    9
    Rep Power
    0

    Default

    thanks kevin..i think that fixed it..but then..a new error occured..

    unreported exception java.lang.Exception; must be caught or declared to be thrown
    singleTwo = new singleTwo()

    i know I should be trying to find the answer on my own now..but i really need help now..

    thanks again

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default

    It's the same deal- you're calling a method (presumably a constructor) that throws an Exception. You have to catch it, or throw the exception yourself.

    If you want more help, I'd recommend providing an SSCCE that demonstrates the problem in as few lines as possible. You should probably also be more mindful of naming conventions- classes (and constructors) are capitalized, constructors do not have return types, and methods and variables are lowercase. Also, don't forget the code tags next time.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    arc_remiel is offline Member
    Join Date
    Jan 2011
    Posts
    9
    Rep Power
    0

    Default

    thanks kevin..i'll read it and keep it in mind..now that you mention it..that singleTwo should be really a constructor..i was not the one who named it..and I see your point..

    thanks..^_^

Similar Threads

  1. After catching the exception thrown
    By scoobyrox in forum New To Java
    Replies: 2
    Last Post: 09-05-2010, 02:29 PM
  2. Replies: 2
    Last Post: 06-01-2010, 02:58 AM
  3. Unreported exception java.sql.SQLException
    By javamula in forum AWT / Swing
    Replies: 4
    Last Post: 09-29-2009, 02:32 PM
  4. Error: unreported exception java.io.IOException; ??
    By jonsamwell in forum New To Java
    Replies: 5
    Last Post: 08-24-2008, 04:11 AM
  5. Which exception is thrown.....
    By money123 in forum New To Java
    Replies: 1
    Last Post: 07-30-2007, 03:41 PM

Posting Permissions

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