Exceptions


Exceptions

ExceptionExamples.java
BadThingHappened.java
Bunny.java

Security Injection on Exception Handling - read, nothing to turn in.


Classwork

Create a class Student that has instance variables for first and last name and int age and double gpa (grade point average)

In your mutators, throw an IllegalArgumentException if you get an age or gpa outside a reasonable range. 

In a separate main, create a Student, ask the user to enter the student's information, and use what they give you to set the values.

To read in numbers, you may choose to use Scanner's nextInt and nextDouble methods (InputMismatchException), or use nextLine, and then parseInt and parseDouble (NumberFormatException).  Either way, if they give you an invalid value an exception will happen.  

Note: If you choose to use nextInt, and nextDouble, after an exception the Scanner will  use up only the next int/double it comes to and not any spaces or newline after it.  So in this case you need to do .nextLine() as well to clear out the rest of the line IF an exception happens.

Also, if they give you a value in the wrong range, the mutator will throw an exception. (Usually it would be better to check our values before giving them to the mutator, but we will let this happen.)

Catch each possible type of exception separately, and give the user a specific explanation of which problem occurred, for instance, if they give you a number outside the range, tell them what range the value should be in (hint: IllegalArgumentException has a parameterized constructor you can use), if they don't give you a number at all, tell them a number is required.

For both age and GPA, make the user keep trying until you get valid input.  Think about how to structure your loops; users get very frustrated if they have to re-enter everything when they only got one input wrong.  (hint: either think about when you know you have to repeat, or when you know you are done, and use an extra variable to keep track of whether you have to try again)

When you have successfully finished setting the Student's information, print the Student.

[EC] Also give Student clone and a copy constructor that throws an IllegalArgumentException if given a null; check in main that these work, catching the exception appropriately.