class Patmatch {
static String strLine="";
public static void main(String [] args) {
try {
FileInputStream fstream = new FileInputStream("c://abc.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br1 = new BufferedReader(new InputStreamReader(in));
while ((strLine = br1.readLine()) != null) {
try {
Patternmatch();
}
catch(NomatchException me) {
System.out.println(me.getMessage());
}
}
in.close();
}
catch(IOException i) {
System.out.println("exec:"+i);
}
}
private static void Patternmatch() throws NomatchException {
Pattern p = Pattern.compile("---");
Matcher m = p.matcher(strLine);
if(m.find()) {
System.out.print(m.start());
}
else {
throw new NomatchException("no match");
}
}
}
class NomatchException extends Exception {
NomatchException(String s) {
super(s);
}
}
In the above code Exception no match is printed twice.
I have abc.txt where i don't have the pattern ---.
I can understand that in that main
i have caught this exception and also in NomatchException()
i have thrown but if i remove the try...catch
it shows error and if the remove in nomatchException()
nothing s displayed.
What should I do now to correct it?