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: Why is this working?

  1. #1
    Junior Member
    Join Date
    Feb 2022
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Why is this working?

    Hi there.
    I was trying some code on java exception handling. I expected it to throw an error, but it returned the arguement.
    What is going on?

     
    public class Main{
     
    public static void main(String[] args) throws Exception {
     
            squarePrinter("I am square");
     
        }
     
     
    public static <Square> void squarePrinter(Square squareObject){
     
            try
            {
     
                System.out.print(squareObject);
     
            }
            catch (IllegalStateException e)
            {
                System.out.println("You cannot print a Java object to the terminal: " + e);
     
            }
        }
     
    }

  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: Why is this working?

    it returned the arguement.
    Please copy the full contents of the command prompt window and paste it here so we can see what happened.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2022
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why is this working?

    Thank you Norm:

    I executed the code from VS Code.
    What you see above is the full content of the code I run.

  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: Why is this working?

    What happens when the program is executed? What is printed on the screen?
    What did you expect to see?

    Note: You have used the word Square in the generic code where normally the coding would use the letter E.
    I believe that your code takes any Object for the args to the method.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: March 18th, 2014, 02:56 AM
  2. Replies: 2
    Last Post: June 25th, 2013, 06:33 AM
  3. Replies: 7
    Last Post: April 25th, 2013, 01:12 PM
  4. Replies: 4
    Last Post: January 27th, 2009, 12:03 AM