• 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 Handling

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ranchers,
Can any one of u ,please clear my confusion regarding the difference between throw & throws clause in ref to Exception Handling?
I am referring to Khalid Mughal & Java2 books for certification .I have tried to read it but still not getting the clear picture.
Thanks in advance,
Trupti.
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The keyword throw can be used to actually throw an exception within your code.
For example...
public static void main(String argv[])
{
int x = 7;
int y = 0;
int z = 0;
if (y == 0)
throw new ArithmeticException();
else
z = x/y;
}

The above code will throw an ArithmeticException at runtime.

The throws clause is used to indicate that a method throws an exception and that whoever calls it must handle the exception that it may throw (if its a checked exception).
For example, RandomAccessFile's constructor throws an IOException. This is evident in the constructor's declaration.
RandomAccessFile(String file, String mode) throws IOException
...that means that if you call the constructor, you must deal with the exception that the method "throws". If you don't, and the exception is a checked exception (not a subclass of RuntimeException), then it will cause a compile time error.
For example, the below code will not compile...
public static void main(String argv[])
{
RandomAccessFile r = new RandomAccessFile("io.txt","rw");
}
Whereas the following code will compile....
public static void main(String argv[])
{
try
{
RandomAccessFile r = new RandomAccessFile("io.txt","rw");
}
catch (IOException e)
{
System.out.println("Exception caught");
}
}
Hope that helped.
Jimmy Blakely
Sun Certified Java Programmer
Travis County Certified Defensive Driver
 
Trupti Samel
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank u jimmy for ur immidiate response .
It has really helped me. Just a small addition to this.
Can catch clause throw exception which are not thrown in try block? I dont think so ,but I am not sure. Am I right?
Thanks in advance,
Trupti.
 
Jimmy Blakely
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good Question...
Yes you can throw an exception within a catch block, however, remember that only exceptions thrown with a try block will be caught in a corresponding catch block.
For example...
___________________________________________
try
{
throw new ArithmeticException(); // first exception thrown
}
catch (ArithmeticException e) // first exception caught
{
System.out.println("First exception caught");
throw new ArithmeticException(); // second exception thrown
}
________________________________________

Because the second exception thrown was not encased within a try block, it will cause an exception to be thrown at runtime.
However, you can put an entire try/catch/finally block within the catch block of another try/catch/finally block, but that only complicates things unnessarily.
Hoped that helped,
Jimmy Blakely,
SCJP
...and always ready to be of assistance to future SCJP's

[This message has been edited by Jimmy Blakely (edited August 29, 2001).]
 
Trupti Samel
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank u very much jimmy.
Regards,
Trupti.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic