An unexpected and unwanted event that disturbs the normal flow of the program is called an exception.
Example
tyrepuncturedException
sleepingException
FileNotFoundException
It is highly recommended to handle exceptions and the objective exception handling is graceful termination of the program.
Exception handling doesn't mean preparing an exception we have to provide an alternative way to continue the rest of the program normally, it is the concept of exception handling
Example
our programming requirement is to read data from remote files locating London. at run time London file not available. our program should not be terminated normally, we have to provide the local file to continue the rest of the normal. this way of defining alternative is nothing but exception handling.
try
{
read data from the remote file
locating at London
}
catch (FileNotFoundException e)
{
use a local file and continue
rest of the program normally
}
Trowable class acts as the root for java exception hitracky throwable class defines two-child class
1.exception
2.errors
Exception
Most of the time Exceptions are caused by our programs under these are recoverable.
Example
If our program requirement is to read the data from remote files locating at London at run time. if a remote file is not available, we will get a runtime exception saying file not found exception. if a file not found, exceptions occur .we can provide a local file and continue the rest of the program normally.
try
{
read data from remote file
locating at london
}
catch (FileNotFoundException e)
{
use local file and continue
rest of the program normally
}
0 Comments