File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on assertion error

 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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 .
 
Andriy Fedotov
Ranch Hand
Posts: 49
 
Swati Kadam
Ranch Hand
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what i need to change to avoid expection.
Can you suggest on it
 
Hamsagayathri Palanisamy
Ranch Hand
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic