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

Thread: unreported exception

  1. #1
    Junior Member
    Join Date
    Jul 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default unreported exception

    Why doesn't my code compile?

    Here's the code:
    public class Exceptions
    {
    	public void method1(int x)
    	{	
    		if (x < 0)
    			throw new Exception("ERROR: Negative value in X coordinate.");
    	}
     
    	public static void main(String[] args)
    	{	Exceptions ex = new Exceptions();
    		try
    		{	ex.method1(-1);
    		}
    		catch (Exception e)
    		{	System.out.print(e.getMessage());
    			e.printStackTrace();
    		}
    	}
    }

    Error message:
    Exceptions.java:6: error: unreported exception Exception; must be caught or declared to be thrown
    throw new Exception("ERROR: Negative value in X coordinate.");
    ^
    1 error
    Last edited by Bao; August 5th, 2019 at 08:35 AM. Reason: Problem solved

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: unreported exception

    Exceptions.java:6: error: unreported exception Exception; must be caught or declared to be thrown
    method1 must either:
    declare that it throws the exception by having a throws clause in its definition
    or it must use a try{}catch block to catch and handle the exception
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 4
    Last Post: February 20th, 2013, 10:01 AM
  2. Replies: 1
    Last Post: February 6th, 2013, 11:21 AM
  3. [HELP] unreported exceptions
    By Paytheprice in forum Java Theory & Questions
    Replies: 3
    Last Post: January 27th, 2013, 08:01 PM
  4. alot of unreported exception
    By Paytheprice in forum Exceptions
    Replies: 1
    Last Post: January 27th, 2013, 09:10 AM
  5. Replies: 6
    Last Post: March 25th, 2011, 03:42 PM

Tags for this Thread