• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

exception handling

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
raj baig
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
raj baig
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
raj baig
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Anu Pillai
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
raj baig
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ...
 
Anu Pillai
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
raj baig
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anu,
Thanks a lot for Sharing The Information.

---
Raj.
 
She said she got a brazillian. I think owning people is wrong. That is how I learned ... tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic