• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Question on assertion error

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 .
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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....
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what i need to change to avoid expection.
Can you suggest on it
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic