gudii9
asked on
Java Exception example issues
if we are multiple catch taking from
parent to child then why we will get Compile time error saying
exception zzz has already been caught
above gives different error as below for some reason?
No exception of type Exception can be thrown; an exception type must be a subclass of Throwable
Not sure what it means by above compilation error?
where below code running fine
parent to child then why we will get Compile time error saying
exception zzz has already been caught
class TestException {
public static void main(String[] args) {
System.out.println("st1");
try {
System.out.println(10 / 0);
} catch (Exception e) {
System.out.println(100 / 2);
}catch (ArithmeticException e) {
System.out.println(100 / 2);
}
System.out.println("st3");
}
}
above gives different error as below for some reason?
No exception of type Exception can be thrown; an exception type must be a subclass of Throwable
Not sure what it means by above compilation error?
where below code running fine
class TestException {
public static void main(String[] args) {
System.out.println("st1");
try {
System.out.println(10 / 0);
} catch (ArithmeticException e) {
System.out.println(100 / 2);
}
System.out.println("st3");
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
class TestException {
public static void main(String[] args) {
System.out.println("st1");
try {
System.out.println(10 / 0);
} catch (ArithmeticException e) {
System.out.println(100 / 2);
} catch (Exception e) {
System.out.println("Not expected exception");
}
System.out.println("st3");
}
}
Always order your exceptions from the more detailed exception to the more generalist.
jglete got the idea..
ASKER
I got it now.
but what compiler says seems different to me?
what above error means? it is not clear
but what compiler says seems different to me?
above gives different error as below for some reason?
No exception of type Exception can be thrown; an exception type must be a subclass of Throwable
Not sure what it means by above compilation error?
what above error means? it is not clear
Try using java.lang.Exception instead of Exception.
If this works, then you have another class Exception in your CLASSPATH, or in the same directory.
Perhaps you were testing exceptions in java and you made a class named exactly Exception.java in your current directory.
If this is the case, I advise you to use another name for that class (like ExceptionTest or similar).
If this works, then you have another class Exception in your CLASSPATH, or in the same directory.
Perhaps you were testing exceptions in java and you made a class named exactly Exception.java in your current directory.
If this is the case, I advise you to use another name for that class (like ExceptionTest or similar).
ASKER
Try using java.lang.Exception instead of Exception.
where i see i had mistakenly named a class as Exception which seems bad thing to do?
I corrected that now and renamed it to ExceptionCls
ASKER
now compiler happy
class TestException {
public static void main(String[] args) {
System.out.println("st1");
try {
System.out.println(10 / 0);
} catch (ArithmeticException e) {
System.out.println(100 / 2);
}catch (Exception e) {
System.out.println(100 / 2);
}
System.out.println("st3");
}
}
class TestException {
public static void main(String[] args) {
System.out.println("st1");
try {
System.out.println(10 / 0);
} catch (ArithmeticException e) {
System.out.println(100 / 2);
}catch (Exception e) {
System.out.println(100 / 2);
}
System.out.println("st3");
}
}
ASKER
public class ExceptionCls {
public static void myMethod(Object x) throws Exception {
if (x == null)
throw new Exception("worst input") ;
}
public static void main(String[] args) throws Exception {
myMethod(null) ;
}
}
Exception in thread "main" java.lang.Exception: worst inputat ExceptionCls.myMethod(Exce
at ExceptionCls.main(Exceptio
why i forced to use throws keyword 2 times one at main method one other at myMethod?
please advise
Any Exception that isn't from the tree of RuntimeException has to be thrown.
You have to do it twice, because each method in the call stack is required to do that.
You have to do it twice, because each method in the call stack is required to do that.
ASKER
Open in new window
i only see ArithmeticException but not exception?
how to and where to interchange?please adivse