• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exceptions Doubt

 
dedcat achi
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class ThrowDemo
{
static void throwOne()
{
System.out.println("Inside throwone");
throw new ArithmeticException();
}

public static void main(String[] args)
{
throwOne();
}
}


Why does this compile fine?

1)Don't we have to handle or declare exceptions which are thrown by methods
using the 'throw' clause??

2) Is there an exhaustive list of an exception hierarchy that we need to memorize for the exam?

Thanks a lot!
 
Ninad Kulkarni
Ranch Hand
Posts: 802
Hibernate Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ArithmeticException is uncheked exception so no need to declare or handle it
if it is other than unchecked exception then it must be declare or handle by method

unchecked exceptions are Error and RuntimeException but error should not be handled because program may not recover from ocuured error

checked exceptions are other than Unchecked Exception

If I am wrong then notify me for that

Regards
Ninad

[ June 22, 2008: Message edited by: Ninad Kulkarni ]
[ June 22, 2008: Message edited by: Ninad Kulkarni ]
 
dedcat achi
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.

You are correct.
However, how do you explain this:

When the IllegalClassFormatException is typed using the throw keyword, then an error message of cannot find symbol is displayed along with its location.

But when the IlegalAccessException is used, I get the error message saying that it should be declared or thrown which should've been the case for above as well.


Now, both are checked exceptions, then why the difference in the error message?
 
Ninad Kulkarni
Ranch Hand
Posts: 802
Hibernate Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
both are checked exception and I got compiler error for both type of exceptions to declare it or handle

Regards
Ninad
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic