• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java exception handling try catch  RSS feed

 
Fauzi Rashid
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the code below I added one try/catch pair to catch "file not found" type erros. I need to add another excpetion handler to detect when the data being fetched from the text file is not numeric (or not int).
The data is stored in a text file (the file name is passed as an argument). The data format is like this:

13
12
10
43
62





 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, what's your question? You'll get better help if you clearly TellTheDetails(←click).

If your question is, "How do I catch more than one exception with the same try block?" then the answer is that you simply list the exceptions one after the other.



If that's not what you're asking, please clarify your question.

 
Vinod suryawanshi
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Fauzi Rashid,
If i am not wrong, you want to detect value your taking having file not found exception and its suppose to be numeric.
Then
1. instead of using try catch use if else loop and detect the value in your variable is numeric or not. You can do that with the help of isdigit, isnumeric function in java.
2. If you want you can read that variable by one character at a time, by function "variablename.charAt(position)" dunction and check that your getting numeric value in it or not.
3. But then also if you want to use try- catch, you can use it. You take that value in string variable first. Then inside try block write code, "Integer.paseInt" method to convert that string into number. if you got any error then that will be catch in catch blog.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinod suryawanshi wrote:Dear Fauzi Rashid,
If i am not wrong, you want to detect value your taking having file not found exception and its suppose to be numeric.
Then
1. instead of using try catch use if else loop and detect the value in your variable is numeric or not. You can do that with the help of isdigit, isnumeric function in java.


No, if you're looking for something to be a valid number, then just try to parse the number and catch the exception. There's no need to duplicate the work that the parse methods are already doing, and we're going to end up calling them any way to turn the string into a number. There's no point it writing a bunch of code to test first whether it will succeed.

2. If you want you can read that variable by one character at a time, by function "variablename.charAt(position)" dunction and check that your getting numeric value in it or not.


You can, but shouldn't. See above.
 
Fauzi Rashid
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Vinod suryawanshi wrote:Dear Fauzi Rashid,
If i am not wrong, you want to detect value your taking having file not found exception and its suppose to be numeric.
Then
1. instead of using try catch use if else loop and detect the value in your variable is numeric or not. You can do that with the help of isdigit, isnumeric function in java.


No, if you're looking for something to be a valid number, then just try to parse the number and catch the exception. There's no need to duplicate the work that the parse methods are already doing, and we're going to end up calling them any way to turn the string into a number. There's no point it writing a bunch of code to test first whether it will succeed.

2. If you want you can read that variable by one character at a time, by function "variablename.charAt(position)" dunction and check that your getting numeric value in it or not.


You can, but shouldn't. See above.



Thanks for the insight. Sorry if I was not clear. I am just starting to learn java.
I got it to work. Here is the code:

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool. Glad you got it sorted out!
 
Fauzi Rashid
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinod suryawanshi wrote:Dear Fauzi Rashid,
If i am not wrong, you want to detect value your taking having file not found exception and its suppose to be numeric.
Then
1. instead of using try catch use if else loop and detect the value in your variable is numeric or not. You can do that with the help of isdigit, isnumeric function in java.
2. If you want you can read that variable by one character at a time, by function "variablename.charAt(position)" dunction and check that your getting numeric value in it or not.
3. But then also if you want to use try- catch, you can use it. You take that value in string variable first. Then inside try block write code, "Integer.paseInt" method to convert that string into number. if you got any error then that will be catch in catch blog.


I see what you are saying. I prefer to use the try/catch, since it seems to be cleaner/simpler.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
A try-catch is a very inefficient way to mimic the isNumeric() method. Don’t use Exceptions as an alternative to if-elses, or methods which return true/false. In the case of parseInt the Exception thrown may be simpler, however. Note there are some bona fide numbers which will produce that Exception from parseInt: this is one:-
12345678901234567890
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch
A try-catch is a very inefficient way to mimic the isNumeric() method.


Interesting. What class defines that? Is it new in 7?

Don’t use Exceptions as an alternative to if-elses, or methods which return true/false.


I'm gonna have to go ahead and disagree with you there.

If we're going to turn it into a number, we're going to have to call a parse method anyway, so there's no point in testing it first. As for "inefficient," that shouldn't really be an issue in most cases. We assume that are input is mostly good, and that bad input is, er, exceptional. Catching exceptions will only slow us down if there are a lot of them, and if there are that many that it's affecting performance, we've got some seriously polluted input to start with, and we need to look into getting it cleaned up or adjusting our assumptions.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are not disagreeing as much as you think; I did say that in the case of parseInt the exception thrown may be simpler.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!