Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

excpetion with multiple catch

Hi,

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();
		}
	}
}

Open in new window


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
Avatar of CPColin
CPColin
Flag of United States of America image

Do you have another class called Exception lying around somewhere that doesn't extend Throwable?
Avatar of gudii9

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.
Avatar of gudii9

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();
		}
	}
}

Open in new window


looks like there is one other Exception class.
above compiled and ran fine and gave below outpt
java.lang.ArithmeticException: / by zero
      at simple.servlet.TestExceptionMulti.main(TestExceptionMulti.java:5)



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.
what is meaning of above error. I have not understood? how you conclude as below based on above

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.
Avatar of gudii9

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.

Open in new window

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 Throwable

The method printStackTrace() is undefined for the type Exception.
no right?

I wonder how you came to right conclusion with ambiguous error message?
ASKER CERTIFIED SOLUTION
Avatar of CPColin
CPColin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of gudii9

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?
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.