gudii9
asked on
excpetion with multiple catch
Hi,
When i try running below code
i see below 2 compilation errors. how to resolve.
No exception of type Exception can be thrown; an exception type must be a subclass of Throwable
The method printStackTrace() is undefined for the type Exception.
class TestMulti2 {
public static void main(String[] args) {
try {
System.out.println(10 / 0);
} catch (Exception e) {
e.printStackTrace();
} catch (ArithmeticException e) {
e.printStackTrace();
}
}
}
above also gives errors
No exception of type Exception can be thrown; an exception type must be a subclass of Throwable
The method printStackTrace() is undefined for the type Exception
please advise
When i try running below code
class TestExceptionMulti {
public static void main(String[] args) {
try {
System.out.println(10 / 0);
} catch (ArithmeticException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
i see below 2 compilation errors. how to resolve.
No exception of type Exception can be thrown; an exception type must be a subclass of Throwable
The method printStackTrace() is undefined for the type Exception.
class TestMulti2 {
public static void main(String[] args) {
try {
System.out.println(10 / 0);
} catch (Exception e) {
e.printStackTrace();
} catch (ArithmeticException e) {
e.printStackTrace();
}
}
}
above also gives errors
No exception of type Exception can be thrown; an exception type must be a subclass of Throwable
The method printStackTrace() is undefined for the type Exception
please advise
Do you have another class called Exception lying around somewhere that doesn't extend Throwable?
ASKER
Do you have another class called Exception lying around somewhere
in same package or in same workspace?
i do not remember creating class with name Exception?
The only way you could be getting the errors you're seeing is if another class called Exception exists in your workspace.
ASKER
package simple.servlet;
class TestExceptionMulti {
public static void main(String[] args) {
try {
System.out.println(10 / 0);
} catch (ArithmeticException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
looks like there is one other Exception class.
above compiled and ran fine and gave below outpt
java.lang.ArithmeticExcept
at simple.servlet.TestExcepti
what is meaning of above error. I have not understood? how you conclude as below based on above
No exception of type Exception can be thrown; an exception type must be a subclass of Throwable
The method printStackTrace() is undefined for the type Exception.
The only way you could be getting the errors you're seeing is if another class called Exception exists in your workspace.
The only possible way to have that error is if there exists a class called Exception somewhere in your classpath that does not extend Throwable. The java.lang.Exception class does extend Throwable, so your code must be referring to some other class.
ASKER
The only possible way to have that error is if there exists a class called Exception somewhere in your classpath that does not extend Throwable.
yes. you are right. I renamed and corrected that.
The java.lang.Exception class does extend Throwable, so your code must be referring to some other class.
but does error say java.lang.Exception does not extend Throwable?No exception of type Exception can be thrown; an exception type must be a subclass of Throwableno right?
The method printStackTrace() is undefined for the type Exception.
I wonder how you came to right conclusion with ambiguous error message?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
No exception of type Exception can be thrown;
This part is saying a class called "Exception" is a problem.
where i have thrown in my code in the catch block?
https://www.google.com/search?q=exception+class+hierarchy&tbm=isch&imgil=HMS6uiB_Y5DkkM%253A%253BtmAOmPU4Kyln6M%253Bhttp%25253A%25252F%25252Fdocstore.mik.ua%25252Forelly%25252Fjava%25252Flangref%25252Fch09_04.htm&source=iu&pf=m&fir=HMS6uiB_Y5DkkM%253A%252CtmAOmPU4Kyln6M%252C_&usg=__obeWkdo4Udc1DKqrmjXwUb1w1SM%3D&biw=1366&bih=653&ved=0ahUKEwibs4TCvdXMAhWE2SYKHZVbDUsQyjcIMw&ei=lPU0V5uZHoSzmwGVt7XYBA#imgrc=HMS6uiB_Y5DkkM%3A
i see all exception and error extend Throwable
how to be more smarter than compiler to understand its language?
The compiler is essentially saying this:
There is a problem with this catch statement because No exception of type Exception can be thrown from the code in the try block.I put the part the compiler actually said in bold. I filled the rest in from experience.
ASKER
Exception class which i renamed to something else should not be there in the workspace of the eclipse right?
if it is in different workspace then i do not get this error right?
i know it is bad bad practice to name a class as Exception.
just for curiosity sake asking?
if it is in different workspace then i do not get this error right?
i know it is bad bad practice to name a class as Exception.
just for curiosity sake asking?
I don't know what you're asking. If you want to know if something will work, you should try it and see what happens. That's how you gain experience.