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

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

Open in new window



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

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of jglete
jglete

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

ASKER

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

Open in new window


i only see ArithmeticException but not exception?

how to and where to interchange?please adivse
Avatar of jglete
jglete

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

Open in new window


Always order your exceptions from the more detailed exception to the more generalist.
jglete got the idea..
Avatar of gudii9

ASKER

I got it now.
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).
Avatar of gudii9

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

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");
      }
}
Avatar of gudii9

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

Open in new window

Exception in thread "main" java.lang.Exception: worst input
      at ExceptionCls.myMethod(ExceptionCls.java:6)
      at ExceptionCls.main(ExceptionCls.java:10)

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.