aspose file tools*
The moose likes Java in General and the fly likes Ingore exceptions in a code segment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Ingore exceptions in a code segment" Watch "Ingore exceptions in a code segment" New topic
Author

Ingore exceptions in a code segment

Johannes Salter
Greenhorn

Joined: May 02, 2010
Posts: 15
I'm looking for a easy way to ignore parsing exceptions in a block of Code.
If an exception is encountered the code should just continue in the next line.

Code:Ideas?

Thanks!
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
There is no easy answer.

Seems to me it would be a mistake to just catch and discard NumberFormatException - you should at least log the detail message.

What is your plan to provide default values?

If I had a whole bunch of these I might try something like a method:



Bill
Gian Franco
blacksmith
Ranch Hand

Joined: Dec 16, 2003
Posts: 977
Hi Johannes,

First of all a disclaimer... Never ignore exceptions...

For your question you could do something in these lines...




"Eppur si muove!"
Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 274

Gian Franco wrote:Never ignore exceptions


Well, I wouldn't say never...



SCJP 6 || SCWCD 5
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

William Brogden wrote:



Off-topic, sorry: I don't know how many times I've written something like this, and scratched my head at the weird error messages it provokes ("identifier expected", "';' expected", etc.) before realizing that once again I've used the keyword "default" as a variable name!


[Jess in Action][AskingGoodQuestions]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
The fact that the display showed the word default in color should have given me a clue!

Yeah, I've used those handy words before and gotten stung too.

Bill
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Michael Angstadt wrote:
Gian Franco wrote:Never ignore exceptions


Well, I wouldn't say never...



I think I would. What harm is there in reporting the exception if there is one? You don't need to throw it, but give the unfortunate developer who is maintaining your code come chance to workout what is happening.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 274

Paul Sturrock wrote:
Michael Angstadt wrote:
Gian Franco wrote:Never ignore exceptions


Well, I wouldn't say never...



I think I would. What harm is there in reporting the exception if there is one? You don't need to throw it, but give the unfortunate developer who is maintaining your code come chance to workout what is happening.


There is no harm in terms of performance of the application. But I think that it does clutter your code and that it actually makes it more difficult for developers to maintain. For example, not only must they read the method call, but they must also spend the time to notice that it's enclosed within a try/catch block. Then, they must look at all the exceptions that are caught and how the application handles them. Plus, it is annoying as a programmer to have to write this extra code in cases such as this because you know that POST is a valid HTTP method and that the exception will, for all practical purposes, never be thrown.

But yes, I think that it's good to tell newer programmers that it is important to handle ALL exceptions because on the whole, this is true.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Well, we may have to agree to disagree, but I can see your code example causing more confusion than any perceived code clutter (though I'm not sure how much clutter the line "logger.warn(e);" would cause) when you make a request to a web server which has been configured to refuse requests using the POST method. Seems a daft thing to do, which makes it unlikely, and hence makes the information you could report in your application all the more important.
Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 274

Paul Sturrock wrote:...when you make a request to a web server which has been configured to refuse requests using the POST method.


I don't think that the event you described is what causes the ProtocolException to be thrown. The Javadocs say that it is thrown "if the method cannot be reset or if the requested method isn't valid for HTTP." This means that if you were to pass something like "FOOBAR" into setRequestMethod(), then the exception will be thrown, because "FOOBAR" is not an HTTP method. I don't think that the exception has anything to do with the given URL not supporting the HTTP method.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Ingore exceptions in a code segment