• Post Reply Bookmark Topic Watch Topic
  • New Topic

Creating an Exception  RSS feed

 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to produce the message in an Exception using try and catch.

Only Vehicle Record objects can be stored.

I thought the following code would produce this but unfortunately it doesn't. Could someone help me understand why?



The output to screen when compiling is as follows:

TestApp.java:759: store(VehicleRecord) in VehicleDB cannot be applied to (Employee)
recordTest.store(recordEmp);
^
Surely to create the message 'Only Vehicle Record objects can be stored' I need to try and store something in VehicleDB which is not a VehicleRecord, hence I tried to store an Employee
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume your store method only takes a VehicleRecord object? (I can't remember). That's what the compiler is telling you anyway. You can allow objects other than VehicleRecords if you define a more general object type in the method signature, with Object being the most general. However, if you have a method which only accepts VehicleRecord objects it is probably correct to define it as you must have.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The good news is that the store signature catches your attempt to store something else at compile time instead of run time. That makes your code safer in case you forget to check for the right type before storing, and makes your code shorter & simpler because you don't have to do that. All around a win-win. Do you agree?
 
Rajagopal Manohar
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Maureen Charlton:
[QB]I am trying to produce the message in an Exception using try and catch.

I thought the following code would produce this but unfortunately it doesn't. Could someone help me understand why?



If you really want to create an exception just to see how exception handling works you can Type cast the object

like
recordTest.store((VehicleRecord)recordEmp);

then it will throw a exception

but as Stan said java does type checking at compile time to ensure you dont inadvertantly make errors.


So dont type cast unless you are sure that it is casted to a correct type.
 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are all so great!

Thank you very much for your excellent and quick responses. Much appreciated!
 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm back again. Sorry to trouble you.

I'm still have difficulties with outputting the error "Only Vehicle object Records can be stored".

I think maybe I'm being a little dumb and I apologise if this is the case. I don't normally have any difficulty producing errors.

My program is working fine BUT I don't want it to.....

I have used the type casting of an object successfully.

My code in my test cast is as follows:



my store method in the VehicleDB class is as follows:

//method to store the vehicle registration
public void store (VehicleRecord vr) throws InvalidRecord

Which is what we are told we are NOT to change; we have to use this.

And the result output to screen I get is:

VehicleRecord details:
======================
Registration Registration: Car Car Car
Number: Date: Make: Model: Colour:

1234567 0185 Maureen Charlton UK

Validating Registration Date: 0185
The length of the date input by user is: 4
Length of date successfully validated i.e. four digits or characters
VALIDATING YEAR
newYear is: 1985
Finished validating Registration
VALIDATING MONTH
newDate is: 01
Registration Month successfully validated
VALIDATING REGISTRATION NUMBER: 1234567
The length of the reg input by user is: 7
Length of registration: VALID i.e is 7 characters

VALIDATING FORMAT OF REGISTRATION:
First Character is NOT a letter
Invalid Registration Number Format

Working like a dream!!! Which is NOT what I wish to have.

I would like an Exception to be caught but it isn't happening. I have no errors. Not something I have a problem with normally!

Am I missing something BIG time here?
 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I tried the following in my store method in the VehicleDB class:



Expecting to get "Only Vehicle Record object can be stored." when passing the Employee record in. i.e.

VehicleRecord test = new Employee ("1234567", "0185", "Maureen", "Charlton", "UK");
VehicleDB record32 = new VehicleDB (test);
test.display( );
try {
record32.store(test);
}//end try block
However I get:
This is a Vehicle Record object so it will be stored can be stored.

For absolutely everything.

Output to screen is:
Car Colour has an empty String value. This record will not be stored.
This is a Vehicle Record object so it will be stored can be stored.
null value not permitted for Car Colour. This record will not be stored.
Entry Set is:
[NV52SSD=NV52SSD 1003 Ford Mondeo White, QP43SSX=qp43SSX 100
3 Ford Fiesta Pink, ZZ43SSX=ZZ43SSX 0182 Ford Fiesta
Yellow]
The number of records within the collections is: 3

Finished!!!

VehicleRecord details:
======================
Registration Registration: Car Car Car
Number: Date: Make: Model: Colour:

1234567 0185 Maureen Charlton UK
This is a Vehicle Record object so it will be stored can be stored.

Am I going mad? Or am I getting so tired I should call it a day?
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you wondering why this works?

test is a reference to Employee which IS-A VehicleRecord. So, this is perfectly valid as you can always pass a subclass into a parameter whose type is its superclass.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!