Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-07-2008, 10:03 AM
Senior Member
 
Join Date: Nov 2007
Posts: 111
bugger is on a distinguished road
Main method with throws Exception
I want to know if it is good idea to use throws Exception with main method:

Code:
public static void main(String[] args) throws Exception { ...
Code:
public static void main(String[] args) throws SQLException { ...
I believe its a quick way to program, because in the other case, you have to write many try catch blocks.

Is this a right approach?

Chao
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-07-2008, 11:04 AM
roots's Avatar
Moderator
 
Join Date: Jan 2008
Location: Dallas
Posts: 251
roots is on a distinguished road
General rule of exception handling is that you should catch exception if you need to so that you can correct the problem causing the exception or you have better way of showing the exceptional case. This applies with main method as well.
When you are catching an exception you should answer why are you catching the exception as well.
__________________
dont worry newbie, we got you covered.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-07-2008, 01:51 PM
Senior Member
 
Join Date: Nov 2007
Posts: 111
bugger is on a distinguished road
Makes sense...
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-07-2008, 03:48 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 296
tim is on a distinguished road
Careful though
Hello bugger.

Be careful when you use the throws keyword. It means that the user of your methods will need to worry about it. If your main() method throws exceptions it could mean that its caller may have problems with it and your program can crash. Remember that if you throw an exception then you must catch it as well.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Trouble with factory method - unhandled exception type Exception desmond5 New To Java 1 03-08-2008 07:41 PM
Main method throwing specific Exception bugger New To Java 3 01-22-2008 04:08 AM
throws Exception javaplus New To Java 1 11-06-2007 08:32 PM
ERROR: Exception in thread "main" java.lang.NoSuchMethodError: main barney New To Java 1 08-07-2007 08:10 AM
exception in thred main java.lang.nosuchmethoderror: main fernando Java Applets 1 08-06-2007 10:11 AM


All times are GMT +3. The time now is 12:36 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org