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

Exception

 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



The above code says that funny() method would cause an exception and that exception would not be handled by funny(). But it would be handled by the Line 1 of the fun() method.


Now, i have seen in some programs as ...
public static void main(String arg[]) throws Exception

I understand that main would cause an exception . But it is not handling the exception.If it is not handling the exception ,then what is the point of saying that it would generate an exception???

Explain it to me and correct it if i have missunderstood anything.
[ November 19, 2008: Message edited by: James Tharakan ]
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds as if a trip to the Java Tutorials would be profitable.

You can do three things with a potential Exception.
  • Ignore it. This is only an option with unchecked Exceptions.
  • Handle it. This is usually done with "try-catch."
  • Declare that you are not handling it. This is done with "throws."
  • You must do one of those three things; since Exception is checked, the "ignore it" option is no longer available, so you must handle it or declare it.

    The main . . . throws Exception (not exception) declaration tells the compiler that you have decided not to handle that Exception in your main method (whether it arose in the main method or elsewhere), so the JVM has to handle it. If you look in Bruce Eckel Thinking in Java you find the JVM does two things then:
  • Print a stack trace.
  • Terminate the Thread the Exception arose in. If it is a single-threaded application the whole application terminates.
  •  
    reply
      Bookmark Topic Watch Topic
    • New Topic