• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception question

 
tvs sundaram
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<ubb>
public class examprep
{
public void first() throws Exception
{
System.out.println("Inside the first method!!");
throw new Exception();
}
public void second() throws AWTException
{
try
{
System.out.println("Inside try block of the second method!!");
first();
}
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();
try{
ep.second();
}
catch(AWTException ee)
{
System.out.println("Catch block of main method");
}
}
}
</ubb>
----------------------------
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.
regards.
tvs sundaram
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic