File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Code alternative Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Code alternative" Watch "Code alternative" New topic
Author

Code alternative

Sindhu Kodoor
Ranch Hand

Joined: Sep 03, 2010
Posts: 65
Hi ,

I have a method that converts the given input to the required format,:



But "Calendar inputDate" is not the only input to be formatted, it can come in of datatype "Date" or of "String", how do I do that?please suggest
Raymond Tong
Ranch Hand

Joined: Aug 15, 2010
Posts: 230
    
    2

method overloading
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Some suggestions:
- rename your method to "toRequiredFormat" or simply "format". Java supports method overloading, so you don't need to specify twice that this method takes a Calendar (once in the parameter list, once in the name).
- don't throw Exception; throw ParseException instead as that's the actual exception type being thrown.
- don't ever write code like this again:
You will loose all information on the message except the message. Better alternatives:
or even simpler


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Then make it this:
Because then in JDK 7 (8?) the safe re-throw concept will kick in.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4363
    
    8

Wouter Oet wrote:Because then in JDK 7 (8?) the safe re-throw concept will kick in.

Interesting - I've not heard of that. I did a quick search, but didn't find anything very specific. Do you have any good references that will expain what that involves?
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

I'll show an example:
This won't compile under JDK 6 because you throw an exception but don't declare it. Under JDK 7/8 the compiler understands that the value of e is a CheckedException or a Runtime exception thus it's valid to throw it. The key is that e must be final otherwise it's value can change in the catch block.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Looks weird to me. It makes sense, as the compiler realizes the only values for e can be CheckedException or a RuntimeException, but that doesn't make it look better. It just looks like you're throwing Exception and therefore the method should declare to throw Exception. A lot of programmers will get confused when they read such code.

I'd prefer one of the other changes to catching exceptions that were planned: multicatch:
In the end, I agree with the final notes in this article: multicatch is awesome, final rethrow is weird.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

I agree with you. Multi-catch is great and safe re-throw is nice. Multi-catch shall be used much more often by programmers than the re-throw functionality.
But the re-throw functionality is great for wrapping method calls and I assume that it will mainly be used by frameworks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Code alternative