Error Handling in Java


You cannot afford to have an application that stops working or crashes if the requested file is not present on the disk. Traditionally, programmers used the return values of methods to detect error that occurred at run time. Error handling becomes a necessity when developing applications that need to take care of unexpected situations. The unexpected situations that may occur during program execution are:
  • Running out of memory
  • Resource allocation errors
  • Inability to find files
  • Problems in the network connectivity

Exception

An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions during the execution of a program. Exception handling code can perform the following tasks.
  • Find the problem(Hit the exception)
  • Inform the error has occurred(Throw exception)
  • Receive the error information(Catch the exception)
  • Take corrective actions(Exception handling)
Inside the standard package Java.lang, java defines several exception classes. The most general of these exceptions are subclasses of the standard type Run time Exception. Some of the built in exceptions are listed below:
  • ArrayIndexOutOfBoundsException
  • ArithmeticException
  • ClassCastException
  • IllegalArgumentEception
  • IllegalStateException
  • NullPointException
  • NumberFormatException
All exception classes are subtypes of class Exception. This class derives from the class Throwable (which derives from the class Object). There are two subclasses that derive from Throwable: Exception and Error. Error represents unusual situations that are not caused by program errors, and indicate things that would not normally happen during program execution.
ex:- JVM running out of memory
Errors are technically not exceptions because they do not derive from class Exception. In general, an error represents something that  happens not as a result of a programming error, but rather because some resource is not available or some other condition required for correct execution is not present.Programs can be written to recover from Exceptions,but programs can't be written to recover from Errors.




Comments

Popular posts from this blog

Viewing Github profile using OAuth in Java

How to setup GitHub using SourceTree?

Security Protection Mechanisms to Safeguard Web Applications