| Forums.Sureshkumar.net : A Perfect Place to Share Knowledge Blogs Games Magazines |
|
|
#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
|
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. |
|
|
|
| The Following User Says Thank You to surekhav For This Useful Post: |
suganyak (07-02-07)
|
|
|
#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
|
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 |
|
|
|
|
|
#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
|
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 <:user-defined-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 <:user-defined-exception> } catch( { //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. |
|
|
|
|
|
#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
|
Re: throw and catch exceptions?
well said prashantu, I have a small doubt, what is difference between the throw and throws keywords?
|
|
|
|
|
|
#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
|
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 :-) |
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| java notes | naga | JAVA Technologies | 5 | 22-09-06 10:30 PM |