Looking For Anything Specific?

ads header

What is Exception Handling?


 

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

}


Errors

 Most of the time, our programs are not caused by our programs due to lack of system resources.Errors ae nonrecoverable. if out of memory error occurs being programmer we cant do anything . and the program will terminate up normally. System admins is the response to increasing memory.


Checked exception vs UnChecked Exception

the exception which are checked by compiler for smooth execution of the program are called checked exceptions.
then compulsory we should handle that checked exception (either by try-catch or throws ) otherwise we will get a compile-time error
the exceptions which are not checked by compiler whether programmer handling or not such type of exceptions called unchecked exceptions 
runtimeExceptions and its child classes, error, and its child classes are unchecked.
except these remaining are checked 





Post a Comment

0 Comments