Forums Register Login

exception handling

+Pie Number of slices to send: Send
hi,
i am trying to handle exceptions using " throw "



when i compile and run example 1 it is displaying : Requested Resource is not available . But look at example 2

+Pie Number of slices to send: Send
I think that the class that you are trying to load using Class.forName() statement is not found by the classloader so a ClassNotFoundException is thrown before reaching the second statement where you are throwing the exception.
Make the class you are trying to load available for the classloader and it will work as you expected.
+Pie Number of slices to send: Send
hi kamesh,

i have provided the unavailable class in Class.forName("com.java.User..");

i want the error message to be displayed what ever i throw.

did you get me ?
+Pie Number of slices to send: Send
As Kamesh said, Since you are trying to load a class thats not present, the compiler throws the exception before you throw it. ie, the second line is not getting executed. If you want to throw your exception in this case, give your throws clause in the catch block. And the code that calls this method will receive your error message.
+Pie Number of slices to send: Send
hi Anu,
Thank you for reply.

if you check my example 1: i have provided throw in try block not in catch block ,even though it is giving the error message as provided in throw .

But for example 2 : As You said , i have provided throw in catch and it is working fine. But for why not in try block.

FileNotFoundException is working in try block
ClassNotFoundException is not working in try block

can you tell me why.

Regards,
Raj.
+Pie Number of slices to send: Send
hi Anu,
Thank you for reply. (i know there is no class available. just leave about class. go with the exception to handle).

if you check my example 1: i have provided throw in try block not in catch block ,even though it is giving the error message as provided in throw .

But for example 2 : As You said , i have provided throw in catch and it is working fine. But for why not in try block.

FileNotFoundException is working in try block
ClassNotFoundException is not working in try block

can you tell me why.

Regards,
Raj.
+Pie Number of slices to send: Send
In Example 1, when you are trying to access the file, I think that the file is present (even if the file is not present, File f = new File() creates the file), and there is no FileNotFoundException. But you are forcefully throwing an exception. In that case the throw statement is executed.

In Example 2, there is a ClassNotFoundException, as you are trying to load a class which is not present. In this case, the control goes to the catch block rather than executing the second line (in which you throw the exception). I hope I make sense to you.
[ December 27, 2006: Message edited by: Anu Pillai ]
+Pie Number of slices to send: Send
hi Anu,
yah make sense.
you mean to say that the statement won't execute throw ( 2 line) and this should be declared in catch block.

Then throw is not useful in try block ??
It should be declared in catch block ...
+Pie Number of slices to send: Send
Throw clause usually dosent make any sense in the try block. You have to give your throws clause in the catch block so that any exception caught can be wrapped in your custom exception and be thrown to any other routine calling it.

There might be situations where you want to throw an exception where there is no real exception, but you want to caution your user, in that case you can throw your exception in the try block itself.
+Pie Number of slices to send: Send
Hi Anu,
Thanks a lot for Sharing The Information.

---
Raj.
Just let me do the talking. Ahem ... so ... you see ... we have this tiny ad...
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 750 times.
Similar Threads
can we write junit testcase for any java class?
Multithreading client socket throws Stream corrupted exception
java exceptions
classnotfound Java->mysql jdbc problem!
I/O package
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 23:14:29.