Try... catch in Java


Hi everyone! Can anyone help me with this project. I found this code writing in a book but cant recognize what is it for. Thanks!

this is just to know how to take input through buffer reader package

1 Like

this is used to check if the string contains only integer or not. using exception handling

Bufferedreader are objects which we use to take input from the user.
Here try and catch is used so that suppose while you are taking input from the user and if he gives a wrong input which means it may enter a string so a runtime error will happen…so to avoid that we use exception handling (Try & catch) .First we try if the code could is in the right format or not and if is is not then it will go to the catch part and it will print “invalid format”.Using this it becomes easy for the user to understand his/her error.

I hope so this answer was useful.

1 Like

is this like equivalent for the isalpha() or isalnum() type of functions?

No. That is about error handling in java.

So, it means, if the user types the wrong format, try… catch is used to handle the error message, so that it will not be printed(error)?

yes its used to handle errors.Suppose if our logic is not working then from try it will come to catch and there it will print “Invalid Input”. This will help the user to understand what mistake he/she have made in a simple language .

1 Like