The document discusses exception handling in Java. It defines an exception as an indication of a problem during program execution. It describes unchecked exceptions like NullPointerException and checked exceptions like IOException. It explains that exceptions can be handled using try, catch, finally, throw, and throws keywords. Exceptions provide a stack trace with information about the exception and point of failure. Exceptions not caught will result in program termination unless handled appropriately.
2. Exception Handling
Exception
• An exception is an indication of a problem that occurs during a program execution.
• Information displayed in response to invalid input from user is called stack trace which includes names
of exception in a descriptive message.
• Initial point which describes the line number due to which exception occurs is called throw point.
• An uncaught exception is one for which there are no matching catch blocks.
• After executing a catch block, program flow of control proceeds to the next statement after last catch
block.
• For forcefully exit from program, call System.exit method with 0 or 1.
• Multi-catch is to handle multiple exceptions in one catch handler.
Instructor: Tanzila Kehkashan
2
3. Exception Handling
Types of Exceptions
• Unchecked Exceptions
• Subclasses of Runtime Exception and Error.
• Does not require explicit handling
• Run-time errors are internal to your program, so you can get rid of them by
• debugging your code
• For example, null pointer exception; index out of bounds exception; division by zero exception, runtime
arguments.
• Checked Exceptions
• Must be caught or declared in a throws clause
• Compile will issue an error if not handled appropriately
• Subclasses of Exception other than subclasses of Runtime Exception.
• Other arrives from external factors, and cannot be solved by debugging
• Communication from an external resource - e.g. a file server or database
How Java handles Exceptions
• Java handles exceptions via 5 keywords. Try, catch, finally, throw & throws.
Instructor: Tanzila Kehkashan
3