

import java.util.*;
import java.util.regex.*;
class ExceptionExample{
public static void main(String[] args) throws Exception{
boolean check=false;
Scanner input=new Scanner(System.in);
System.out.print("Enter string: ");
String str=input.nextLine();
Matcher m = Pattern.compile("[^\\s^\\w]").matcher(str);
while(m.find()){
check = true;
}
if(check==true){
throw new Exception("InvalidCharacterFoundException!");
}
else{
System.out.println("There is no invalid character in the string.");
}
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.