File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception question 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 "Exception question" Watch "Exception question" New topic

Exception question

tvs sundaram
Ranch Hand

Joined: Jan 28, 2001
Posts: 153
public class examprep
public void first() throws Exception
System.out.println("Inside the first method!!");
throw new Exception();
public void second() throws AWTException
System.out.println("Inside try block of the second method!!");
catch(Exception ex)
System.out.println("Inside Exception catch block of second method!!");
throw (AWTException)ex;
public static void main(String[] args)
examprep ep = new examprep();
catch(AWTException ee)
System.out.println("Catch block of main method");
When I ran the code (Which i collected from browsing ranch and picked up from old thread) above on my jdk1.2.2 in Win 98, my compiler complains that
class AWTException not found in throws..
class AWTException not found etc.,
But there was a long discussion about the run-time casting of the Exceptions in this program. I haven't got head or tail of it.
Please help.
How to interpret this code.
Do we get such complicated codes in the real exam.
Is there any standard way to proceed through the lengthy code keeping track of all references/ variables etc.,
Help is greatly appreciated. Iam giving the exam in monthend.
tvs sundaram
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
First of all, have you imported AWTException? If you haven't, than that would certainly cause compile time problems. Also, you compiler won't catch this, but casting an instance of the Exception class itself into an AWTException is going to give you runtime errors. Casting does not change the type of an object, it just makes it possible to access class/interface specific methods and fields. If you change 'throw new Exception();' to 'throw new AWTException();' you then there won't be a runtime exception.
I agree. Here's the link:
subject: Exception question
It's not a secret anymore!