+ 1
where we can use try catch in java leaving array
I'll be kush if u give example x_x
13 odpowiedzi
0
Ehm, then your keywords must have been strange, because Arrays are a very rare use case... The most exceptions handled in Java should be all sort of IO-stuff and network issues. Parsing exceptions, all database exceptions, casting exceptions, number and date format exceptions. And the probably (hopefully) biggest part are all kind of exceptions, which are used to handle bad input within the code or from interfaces: Runtime, Nullpointer and all sort of user defined exceptions. Here a sample so you can see, that an exception can be placed anywhere:
https://code.sololearn.com/cocuoF2Ta6yc/?ref=app
+ 3
Am really thankful for these explinations,thank you for clearing my doubts🙂
+ 1
I would like to give you an answer, but therefore I need to know, what exactly you mean with "leaving array" (??)...
+ 1
I meant that where ever i searched for this try..catch they were specifically telling for array ,not any explination for other elements
+ 1
According to me you said that we can use it in any io,so here can you check this code and correct it with my mistake mentioned
https://code.sololearn.com/c1mQeUT0F1kI/?ref=app
+ 1
Like what i mean is that it have an error so it must not stop execution of other parts.
0
*no guarantee that this list is complete 😉
0
Here I used plain exceptions to handle invalid input:
https://code.sololearn.com/c8M9Y10ebKAe/?ref=app
0
Here a number format exception:
https://code.sololearn.com/cj9txutZNqAF/?ref=app
0
You can also just fix the little bug in your own code and enter some invalid data as input (e.g. "ABC 3") and you'll have another use case 😉
https://code.sololearn.com/cigIE5GS6M0j/?ref=app
0
You need to put the command, which is causing an exception within the catch block. Here it is not the printing, but the method nextInt() on an input of type string.
int b = 0;
try {
b = sc.nextInt();
0
Btw. if you do as I posted now, you would proceed with your program and cause another exception (division by zero) - but good to train 😁👍
0
There are some errors which require to stop the execution, e.g. it doesn't make any sense to proceed with operations on a database, if there's no connection. You can have exceptions, which you can handle otherwise, e.g. by using default values.