Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exceptions Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exceptions Doubt" Watch "Exceptions Doubt" New topic
Author

Exceptions Doubt

dedcat achi
Greenhorn

Joined: Jun 22, 2008
Posts: 11
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

Joined: Aug 31, 2007
Posts: 791

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 ]

SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
dedcat achi
Greenhorn

Joined: Jun 22, 2008
Posts: 11
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

Joined: Aug 31, 2007
Posts: 791

both are checked exception and I got compiler error for both type of exceptions to declare it or handle

Regards
Ninad
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Exceptions Doubt