Forums.Sureshkumar.net : A Perfect Place to Share Knowledge         Blogs     Games    Magazines    

"Sharing knowledge does not lessen your store, often it gets you more. Sharing plays a key role in relationships and bonding, happens in small steps and is assisted through community membership."

Go Back   SURESHKUMAR.NET FORUMS > TECHNICAL DISCUSSIONS > JAVA Technologies
Register FAQ Members List Calendar Games Blogs Search Today's Posts Mark Forums Read

   

Reply
 
LinkBack Thread Tools Rate Thread Display Modes
Old 07-02-07, 01:55 PM   #1 (permalink)
Senior Member
 
Join Date: Aug 2006
Age: 28
Posts: 275
Thanks: 0
Thanked 5 Times in 5 Posts
Thanks: 0
Thanked 5 Times in 5 Posts
Rep Power: 5 radhika743 is on a distinguished road
throw and catch exceptions?

Friends, Can anybody explain me,"How do you throw and catch exceptions?"

radhika743 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 07-02-07, 02:49 PM   #2 (permalink)
Member
 
Join Date: Jul 2006
Posts: 67
Thanks: 11
Thanked 43 Times in 29 Posts
Thanks: 11
Thanked 43 Times in 29 Posts
Rep Power: 8 surekhav is just really nice surekhav is just really nice surekhav is just really nice surekhav is just really nice surekhav is just really nice surekhav is just really nice
Re: throw and catch exceptions?

Hi Radhika,
U actually put the part of code where there is a chance of exception into a try block and after closing the try block u have ur catch block where u ll be having the message to be printed abt the exception
For eg:
in Data base connectivity
We have many possible exceptions to occur for connection itself
So we usually put the connection establishment in try block

public static Connection getConnection()
{
Connection con=null;
try
{
String ConnectionString=jdbc:oracle:thinDatabaseName:port number:oracl;
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
con=DriverManager.getConnection(ConnectionString,username,password);
}
catch(Exception e)
{
System.err.println(e);//Prints the exception
}
return con;//returns the connection
}

}


Friends correct me if i'm wrong
__________________
Regards
Surekha

Last edited by surekhav; 07-02-07 at 03:07 PM.
surekhav is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
The Following User Says Thank You to surekhav For This Useful Post:
suganyak (07-02-07)
Old 14-02-07, 03:35 PM   #3 (permalink)
Banned
 
Join Date: Oct 2006
Location: India
Age: 25
Posts: 33
Thanks: 1
Thanked 4 Times in 4 Posts
Thanks: 1
Thanked 4 Times in 4 Posts
Rep Power: 0 hemanthjava is on a distinguished road
Re: throw and catch exceptions?

hi the question asked was How do you throw and catch exceptions. We use a "throws:" clause in th method header declaration and catch the exception thrown either in the same method or in the super class invoking method.

For more information check out

Java Exception Handling
hemanthjava is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 14-02-07, 04:57 PM   #4 (permalink)
Senior Member
 
Join Date: Jan 2007
Age: 27
Posts: 103
Thanks: 0
Thanked 7 Times in 5 Posts
Thanks: 0
Thanked 7 Times in 5 Posts
Rep Power: 4 PrashanthU is on a distinguished road PrashanthU is on a distinguished road
Re: throw and catch exceptions?

Radhika...

NOTE : There is some problem while i post using angular brackets so..plz...find <

Hope you are aware of the keywords that you come across Exception Handling...that include : try,catch,finally blocks..throw,throws..clauses...

The below explanation is in view that you are aware of the differences between 'throw' and 'throws'...

We generally come across or use 'throw' when we wish to throw an object of an Exception ( here the exception object thrown will be of the user defined exception class which has to extend the 'Exception' class under the 'Run-time Exception' class which in turn is under the top most exception class 'Throwable' class )....
----------------------
1) First you have to define your own exception class depending upon the requirement extending the Exception class...
as
class &lt:user-defined-exception> extends Exception
{
//....
}
2)During the main code which you will obviously be placing in the 'try' block...for handling specific exceptions...so somewhere in the main code you you have to handle user defined exception in which case you 'throw' and object of that corresponding user-defined-exception..which has to be caught (handled) by 'catch' block...

A simple way of doing it...

SYNTAX:
public class
{
public static void main(String args[])
{
try
{
//statements...

throw new &lt:user-defined-exception>();
}
catch( <user-defined exception> ref-var)
{
//handle the exception by printing custom message or call a method from the user-defined exception class
}
}
__________________
" Success Is Not Measured By What One Has Achieved, But What One Has Achieved, Compared To What One Is Capable Of "
Keep Smiling :-)

Last edited by PrashanthU; 14-02-07 at 05:03 PM.
PrashanthU is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 15-02-07, 02:32 AM   #5 (permalink)
Member
 
Join Date: Aug 2006
Age: 25
Posts: 90
Thanks: 1
Thanked 10 Times in 7 Posts
Thanks: 1
Thanked 10 Times in 7 Posts
Rep Power: 3 rk_ramakrishnamca is on a distinguished road
Re: throw and catch exceptions?

well said prashantu, I have a small doubt, what is difference between the throw and throws keywords?
rk_ramakrishnamca is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 15-02-07, 11:09 AM   #6 (permalink)
Senior Member
 
Join Date: Jan 2007
Age: 27
Posts: 103
Thanks: 0
Thanked 7 Times in 5 Posts
Thanks: 0
Thanked 7 Times in 5 Posts
Rep Power: 4 PrashanthU is on a distinguished road PrashanthU is on a distinguished road
Re: throw and catch exceptions?

Ramakrishna..

On observing one can atleast infer some differences b/w 'throw' and 'throws'
throw --- an action explicitly done
throws --- probability of occurrence of an action

As i said 'throw' is generally used to throw an object of user-defined exception and handle accordingly...where as 'throws' is specified as part of a method definition which has the probability of throwing some Unchecked exceptions and has to be handled by the programmer...in general we use 'throws' clause during File I/O operations,Database Connectivity...

SYNTAX:

void method-name() throws exception1,exception2...
{
//
}
__________________
" Success Is Not Measured By What One Has Achieved, But What One Has Achieved, Compared To What One Is Capable Of "
Keep Smiling :-)
PrashanthU is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 15-02-07, 04:03 PM   #7 (permalink)
Member
 
Join Date: Aug 2006
Age: 25
Posts: 90
Thanks: 1
Thanked 10 Times in 7 Posts
Thanks: 1
Thanked 10 Times in 7 Posts
Rep Power: 3 rk_ramakrishnamca is on a distinguished road
Re: throw and catch exceptions?

Thank you prashantu.
rk_ramakrishnamca is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
java notes naga JAVA Technologies 5 22-09-06 10:30 PM


All times are GMT +6.5. The time now is 02:26 PM.





Search Engine Optimization by vBSEO 3.1.0