Java Exception Handling

In java programming language there is a feature called excepton handling to handle run time errors which are caused by exceptions.
What is an exception in java?
An exception is unwanted event, this causes the normal flow of the program to be terminated. Java automatically generates the system error when the exception occurs
Some Basic examples for exceptions
- opening a unavailable (opening shortcuts present on desktop,where the program is already uninstalled)
- wrong input (entering alphabets instead of numbers)
- Loss of network connection
- Code errors
Hierarchy of Java Exception classes
IMage
Types of exceptions
- Checked exceptions
- Unchecked Exceptions
- Errors
Checked exceptions
All exceptions excluding of run time exceptions are called checked exceprtions. These are the classes which inherits throwable class except run-time exception. These are checked during the compile time. some of the examples are IOException, SQLException
Unchecked expressions
Run time exceptions are know as unchecked expressions because uncked expressions are not checked at the compile time,they are checked at run-time. These are the classess which inherits only RuntimeException class. some of the examples are ArithmeticException, ArrayIndexOutOfBoundsException, NullPointerException
Error
ERROR is a non recoverable exceptions examples are VirtualMachineError, AssertionError, OutOfMemoryError etc
Java Exception Keywords
Keyword | Description |
---|---|
try | try keyword is used to create a block, where we can place the exception code. we cant use try block alone, it must be followed by either catch block or finally block |
catch | the catch block is used to handle the exception. catch block should be after the try block. we cant use catch block alone |
finally | finally block is used to execute import code, whether there was an exception occurred or not |
throw | throw keyword is used to throw an exception to catch block |
throws | throws keyword is used to declared exceptions. this keyword doesn’t throw exceptions |