+ 1
Exceptions
If i asked users to input something int and they wrote it as a String And i want to make an Exception Try&Catch to send for them a massage how could i do it in Java ? Could you wrote the code under
5 Answers
+ 3
Anas Al-kamel
You need to put everything inside try block if you want to catch them.
Copy this code on Sololearn playground and see the effect
import java.util.Scanner;
public class Program
{
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
try {
int n = sc.nextInt();
System.out.println("Hello " + n);
} catch (Exception e) {
System.out.println("Enter only numeric value => " + e);
}
}
}
0
Alphin K Sajan well my code is about send some int and i will repeat it , but if there is something wrong as i mentioned above it will send an message like (âHey you made somthing wrong!â)
could you help me with that ?
int s = read.nextInt();
try{
}catch(Exception e){
System.out.print (âHey you made somthing wrong!â);
}
0
if you can complate the code above and iâll understand :)
0
You can use a while true loop and break after getting a correct input.
Note that you have to use scanner.next() in catch or the program will use your last input instead of asking you for a new one.
https://code.sololearn.com/cae77mMGy50H/?ref=app