• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exception question

 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic