Home
Examples
Labs
Assignment
Marks
Midterm
Final Exam
Resources

Exceptional Situations

When programs access things in the outside world, it is possible that something could go wrong that is beyond the control of the program.  For example, we may get bad input from the user, or try to open a data file that does not exist.

If there is any possibility that something like this could happen, you must explicitly tell your program how to handle it.  The simplest way to handle exceptional situations is by using throws Exception.  Using throws Exception in any method header (including the main method header) tells the method to handle exceptional situations in the default way - basically by crashing the program and printing information about the error on the screen.  Be sure to use throws Exception any time that you will be receiving input, or doing file output.

      public static void main (String[] args) throws Exception
        {
          . . . .
        }
// end main method