• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exception handling

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am having some trouble with understanding the syntax of the new topic i school, exception handling.


The task is :
The method createAgreement, must throw a RuntimeException with an appropriate text, if you try to create an agreement that overlaps the timespan of an existing agreeement. Which means if the new agreeement has a "fromDate" in between the "fromDate" and "endDate" of an existing agreeement. Or if the new "fromDate" is after "fromDate" on an agreement that does not yet have a "endDate" (is null).

The constructor of an Agreement takes only the parameter of the "fromDate", as the "endDate" is not known untill the Agreement is cancelled.


Se my code below, which is obviously not working. My main problem is, that i cannot see how an exception could occur? in my head i want to "Catch" the event of the if's not being true, and an exeption to occur then - but that doesnt seem quite right either
also, the placement of the try/catch, and return statements is abit confusing.

Please help!


 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your requirements say to throw a RuntimeException, instead you are catching one.
 
Anders hofel
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Your requirements say to throw a RuntimeException, instead you are catching one.



True, it guess it should be something like below, bot that doesnt fix it

catch(Exception e){
throw new RuntimeException("Write something");
}
 
Ranch Hand
Posts: 530
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The tasks says that if some conditions do not meet the requirement, you must throw a RuntimeException. So instead of try-catch like in the previous code, you must do:

 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anders hofel wrote: . . . The method createAgreement, must throw a RuntimeException . . .

I am afraid I am an awkward so‑and‑so who makes life difficult for all whom I encounter.

I think you should query that instruction with your teacher; in my opinion, that method should throw a non‑runtime exception. Then the compiler will force the calling method to handle the Exception. You should also get rid of the return null; bit, which is unnecessary and also nulls are potentially dangerous. I think your method should look like thisI cannot understand the logic of your Agreement class. You appear to be passing one date to its constructor, so how do you work out start date and end date? And where does 1st January 2011 come from? Are you adding to a static Agreement class method or to a List called Agreements? If you have a List called Agreements, do you need to return the Agreement object, or can you give the method void return type?
 
Anders hofel
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for answering. I can see the code is messy from trying a bunch of stuf.. will fix it.

The end date is not part of the constructor, but it will be set when the agreement is terminated.

I will stick to the RuntimeException for now to keep it simple


would this be somewhat correct? (leaving out the try statement)









Campbell Ritchie wrote:

Anders hofel wrote: . . . The method createAgreement, must throw a RuntimeException . . .

I am afraid I am an awkward so‑and‑so who makes life difficult for all whom I encounter.

I think you should query that instruction with your teacher; in my opinion, that method should throw a non‑runtime exception. Then the compiler will force the calling method to handle the Exception. You should also get rid of the return null; bit, which is unnecessary and also nulls are potentially dangerous. I think your method should look like thisI cannot understand the logic of your Agreement class. You appear to be passing one date to its constructor, so how do you work out start date and end date? And where does 1st January 2011 come from? Are you adding to a static Agreement class method or to a List called Agreements? If you have a List called Agreements, do you need to return the Agreement object, or can you give the method void return type?

 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anders hofel wrote: . . . I will stick to the RuntimeException for now to keep it simple

That would be a wrong approach. You stick to the runtime exception because youhave been told to is an acceptable approach, but I still think you should query that instruction.

would this be somewhat correct? . . .

No. I showed you what would be correct.

I think you should forget the Exception for the time being and go back to the design of your Agreement class. How do you tell when an agreement starts and finishes? If you only pass one Date object to its constructor, how do you calculate its duration?If your agreements are supposed to be distinct in time, how do you tell whether they overlap? Does the Agreement class have an overlapsWith(Agreement ag) method or similar? Start by finding out whether any of the Agreements overlaps. Then you can print out something like this:-Make sure you have got that working, then you can replace the print call with your Exception. And you do that before you try any adding to Lists.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic