• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Ingore exceptions in a code segment

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
blacksmith
Posts: 979
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Johannes,

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

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



 
Ranch Hand
Posts: 282
Eclipse IDE PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gian Franco wrote:Never ignore exceptions



Well, I wouldn't say never...

 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Michael Angstadt
Ranch Hand
Posts: 282
Eclipse IDE PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 282
Eclipse IDE PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
They gave me pumpkin ice cream. It was not pumpkin pie ice cream. Wiping my tongue on this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic