• Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception  RSS feed

 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • 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 ]
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • 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.
  •  
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!