| Author |
Question on assertion error
|
Dinesh Tahiliani
Ranch Hand
Joined: Aug 06, 2007
Posts: 486
|
|
public class Testdemo { static void test() throws Error { if (true) throw new AssertionError(); System.out.print("test" ); } public static void main(String[] args) { try { test(); } catch (Exception ex) { System.out.print("exception"); } System.out.print("elld"); } } } When i compile i get this Exception in thread "main" java.lang.AssertionError at testdemo.test(testdemo.java:21) at testdemo.main(testdemo.java:28) Can someone expalin me this
|
Thanks<br />Dinesh
|
 |
Dinesh Tahiliani
Ranch Hand
Joined: Aug 06, 2007
Posts: 486
|
|
10 .public class testdemo { 11. static void test() throws RuntimeException { 12. try { 13. System.out.print(�test �); 14. throw new RuntimeException(); 15. } 16. catch (Exception ex) { System.out.print(�exception �); } 17. } 18. public static void main(String[] args) { 19. try { test(); } 20. catch (RuntimeException ex) { System.out.print(�runtime �); } 21. System.out.print(�end �); 22. } 23.} When i used thios it gives me o/p test exception end Can you please expalin me the difference the two .
|
 |
Andriy Fedotov
Ranch Hand
Joined: Mar 07, 2008
Posts: 49
|
|
AssertionError IS-A java.lang.Error java.lang.Object extended by java.lang.Throwable extended by java.lang.Error extended by java.lang.AssertionError RuntimeException IS-A java.lang.Exception java.lang.Object extended by java.lang.Throwable extended by java.lang.Exception extended by java.lang.RuntimeException
|
 |
Swati Kadam
Ranch Hand
Joined: Mar 05, 2008
Posts: 39
|
|
Hii Dinesh.. Exception and Error are subclasses of Throwable... and AssertionError is subclass of Error... In your code, test method throws AssertionError(subclass of Error)..but not handeling that using catch....so, that exception is propagated to the main method...(as main is calling test) now in main method you call test in try block...but you are catching Exception...so it cant't find the matching catch block... so main threads default exception handler prints the message that you see as the output....
|
SCJP / Preparing For SCWCD
|
 |
Dinesh Tahiliani
Ranch Hand
Joined: Aug 06, 2007
Posts: 486
|
|
So what i need to change to avoid expection. Can you suggest on it
|
 |
Hamsagayathri Palanisamy
Ranch Hand
Joined: Mar 10, 2008
Posts: 44
|
|
Originally posted by Dinesh Tahiliani: So what i need to change to avoid expection. Can you suggest on it
Dinesh, Instead of catching "Exception" you need to catch "Error" Hope your problem got resolved now. -Thanks & Regards, Hamsa
|
 |
 |
|
|
subject: Question on assertion error
|
|
|