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: How to catch thrown exception

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

    Default How to catch thrown exception

    Hi , the exception in the below code, how can I catch the exception it without the program crashing

    public void setFirstName(String firstName){
     
    	  if(firstName.length() < 2){
     
    		  throw new IllegalArgumentException("NAME LENGTH IS TOO SHORT! ");
    	  }
    	  this.firstName = firstName;
      }

    I want to catch it in the tester class


    public class Car_Tester{
       public static void main (String[] args){
     
          CarType1 c1 = new CarType1("B.M.W","Series 7",100,51000,true);
          System.out.println(c1.toString());
          c1.move();
     
          try{
            setPrice(int price);
          }
          catch(IllegalArgumentException e)
          {
            System.out.println(" error");
          }
     
       }

    Is it possible to do this? my solution above didn't work.

    thanks

  2. #2
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    281
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: How to catch thrown exception

    In what way didn't it work? Did you get an error message?

    Regards,
    Jim

Similar Threads

  1. How do I log exception when it is thrown
    By huntorotron in forum Exceptions
    Replies: 3
    Last Post: March 26th, 2014, 08:39 PM
  2. Socket closed Exception being thrown.. please help
    By adi0011 in forum What's Wrong With My Code?
    Replies: 17
    Last Post: February 11th, 2014, 05:53 AM
  3. [SOLVED] Copying a file to an array and getting exception thrown for input
    By KristopherP in forum What's Wrong With My Code?
    Replies: 7
    Last Post: September 1st, 2013, 12:07 AM
  4. Replies: 1
    Last Post: February 6th, 2013, 10:21 AM
  5. Exception Thrown
    By Frame in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 29th, 2011, 11:14 PM