Results 1 to 4 of 4
  1. #1
    bugger is offline Senior Member
    Join Date
    Nov 2007
    Posts
    111
    Rep Power
    0

    Default Main method with throws Exception

    I want to know if it is good idea to use throws Exception with main method:

    Java Code:
    public static void main(String[] args) throws Exception {
    ...
    Java 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

  2. #2
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    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.

  3. #3
    bugger is offline Senior Member
    Join Date
    Nov 2007
    Posts
    111
    Rep Power
    0

    Default

    Makes sense...

  4. #4
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default 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. :p
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

Similar Threads

  1. Main method throwing specific Exception
    By bugger in forum New To Java
    Replies: 5
    Last Post: 05-13-2009, 03:34 PM
  2. Replies: 1
    Last Post: 03-08-2008, 07:41 PM
  3. throws Exception
    By javaplus in forum New To Java
    Replies: 1
    Last Post: 11-06-2007, 08:32 PM
  4. Replies: 1
    Last Post: 08-07-2007, 08:10 AM
  5. Replies: 1
    Last Post: 08-06-2007, 10:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •