What in Exception in Java?

By Admin on

Exception in Java

An exception (or exceptional event) is a problem that arises during theexecution of a program. When an Exception occurs the normal flow of the program is disrupted and the program/Application terminates abnormally, which is not recommended, therefore these exceptions are to be handled.

An exception can occur for many different reasons, below given are some scenarios where exception occurs.

  • A user has entered invalid data.

  • A file that needs to be opened cannot be found.

  • A network connection has been lost in the middle of communications or the JVM has run out of memory.

Some of these exceptions are caused by user error, others by programmer error, and others by physical resources that have failed in some manner.

Based on these we have three categories of Exceptions you need to understand them to know how exception handling works in Java,

  • Checked exceptions: A checked exception is an exception that occurs at the compile time, these are also called as compile time exceptions. These exceptions cannot simply be ignored at the time of compilation, the Programmer should take care of (handle) these exceptions.

    For example, if you use FileReader class in your program to read data from a file, if the file specified in its constructor doesn't exist, then anFileNotFoundException occurs, and compiler prompts the programmer to handle the exception.
SPIRO Google Plus