This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception Handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception Handling" Watch "Exception Handling" New topic
Author

Exception Handling

Trupti Samel
Greenhorn

Joined: May 15, 2001
Posts: 24
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.
Jimmy Blakely
Ranch Hand

Joined: Jul 10, 2001
Posts: 57
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

Joined: May 15, 2001
Posts: 24
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

Joined: Jul 10, 2001
Posts: 57
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

Joined: May 15, 2001
Posts: 24
Thank u very much jimmy.
Regards,
Trupti.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception Handling
 
Similar Threads
throws and then comes back...So why unreachable?
do u have any shortcut
Server error handling
Are EJB System Exceptions specific?
Exception handling Not Clear yet