• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Ingore exceptions in a code segment

 
Johannes Salter
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • 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!
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • 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
 
Gian Franco
blacksmith
Ranch Hand
Posts: 979
  • Mark post as helpful
  • send pies
  • 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...



 
Michael Angstadt
Ranch Hand
Posts: 277
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gian Franco wrote:Never ignore exceptions


Well, I wouldn't say never...

 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
Rancher
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • 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
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • 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: 277
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • 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
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • 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: 277
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic