Exception Handling (#28722)

Customized Exception Handling
pull/28773/head^2
saicharanc 2019-01-12 10:28:15 -06:00 committed by Christopher McCormack
parent 848e5d8430
commit bcbf14a25e
1 changed files with 11 additions and 7 deletions

View File

@ -3,12 +3,15 @@ title: Exceptions in Java
---
## What is an Exception?
An exception is an unwanted or unexpected event, which occurs during the execution of a program i.e at run time, that disrupts the normal flow of the programs instructions. Exceptions can be thrown either during run-time or during compile-time.
An exception is an unwanted or unexpected event, which occurs during the execution of a program (i.e, at run time) that disrupts the normal flow of the programs instructions. Exceptions can be thrown either during run-time or during compile-time.
## Exceptions Types
1. Checked Exceptions: Exceptions that are checked during the compile-time.
2. Unchecked Exceptions: Exceptions that are checked during the run-time.
## Customized Exception Handling
Java exception handling is managed via five keywords: `try`, `catch`, `throw`, `throws`, and `finally`. Any exception that is thrown out of a method must be specified as such by a `throws` clause. Any code that absolutely must be executed after a `try` block completes is put in a `finally` block.
## Error vs Exception
* Error: An Error indicates serious problem that a reasonable application should not try to catch.
@ -17,7 +20,7 @@ An exception is an unwanted or unexpected event, which occurs during the executi
Note: It is usually hard to reocover from errors, compared to Exceptions.
## Checked versus Unchecked exception
A checked exception is an exception class that extends `Exception` in its signature. For a checked exception, each method that calls the method that throws a checked exception, will either to handle the exception (with a try-catch clause) or declare `throws` in its method signature. The compiler will complain if the exception is not handled by any method.
A checked exception is an exception class that extends `Exception` in its signature. For a checked exception, each method that calls the method that throws a checked exception, will either to handle the exception (with a `try-catch` clause) or declare `throws` in its method signature. The compiler will complain if the exception is not handled by any method.
Example of a checked exception signature:
```java
@ -33,13 +36,13 @@ public class TooManyItemsException extends RuntimeException { }
## Exception Hierarchy
All exception and errors types are sub classes of class Throwable, which is base class of hierarchy.One branch is headed by Exception. This class is used for exceptional conditions that user programs should catch. NullPointerException is an example of such an exception.Another branch,Error are used by the Java run-time system(JVM) to indicate errors having to do with the run-time environment itself(JRE). StackOverflowError is an example of such an error.
All exception and errors types are sub classes of class `Throwable`, which is base class of hierarchy. One branch is headed by `Exception`. This class is used for exceptional conditions that user programs should catch. `NullPointerException` is an example of such an exception. Another branch, `Error` are used by the Java run-time system(JVM) to indicate errors having to do with the run-time environment itself (JRE). `StackOverflowError` is an example of such an error.
![alt text](https://github.com/AmilaIndika789/Images/blob/master/Java%20Exceptions%20and%20Errors.png "Part of the Java Exceptions and Error Hierarchy")
## Exception Handling
* Use try-catch (or try-catch-finally): Exceptions can be handled by catching specific exceptions using catch clause and handling each exception separately.
* Use throws clause: Exceptions can be thrown without being handled. If an exception happens, it has to be caught by the calling function.
* Use `try-catch` (or `try-catch-finally`): Exceptions can be handled by catching specific exceptions using catch clause and handling each exception separately.
* Use `throws` clause: Exceptions can be thrown without being handled. If an exception happens, it has to be caught by the calling function.
## How to use try-catch clause
@ -62,8 +65,8 @@ finally {
## How to use throws clause
```
public methodName throws ExceptionType1, ExceptionType2{
```java
public methodName throws ExceptionType1, ExceptionType2 {
//some block of code
}
@ -77,3 +80,4 @@ Tip: If you are not really sure of what kind of exceptions your application may
#### More Information:
- [Oracle Java Docs : Exception](https://docs.oracle.com/javase/specs/jls/se7/html/jls-11.html)