Can we specify a catch clause for an exception not thrown in the try block?. How does the compiler make sure that the try block will not raise the exception thatz being specified in one of the catch CLAUSE.
the below example gives a COMPILER ERROR since the try block doesn't throw the BLUEEXCEPTION but is being caught in one of the CATCH clause.
class RedException extends Exception {}
class BlueException extends Exception {}
class White {
void m1() throws RedException {throw new RedException();}
public static void main (
String[] args) {
White white = new White();
int a,b,c,d; a = b = c = d = 0;
try {white.m1(); a++;}
catch (RedException e) {b++;}
catch (BlueException e) {c++;}
finally {d++;}
System.out.print(a+","+b+","+c+","+d);
}}
clarify ~ Shalini