Exception Handling
What is an Exception?
Definition:
“When an unusual, irregular, abnormal or nonstandard condition arises in a sequence of code at run time this phenomenon is referred as Exception handling”
An exception is a runtime error.
In Object Oriented, an exception is an object that is used to describes an exceptional condition that has occurred in a piece of code.
When an exception arises, an object that is rejavasenting that exception is created and thrown in the method that caused the error.
- That method may be used to choose and to handle the exception itself, or it may pass on.
- Either way, at some point the exception is caught and processed.
Some Fundamentals of Exception Handling
Exceptions can be generated by the Java runtime system, or they can be manually generated by in the code, by the user himself.
Exceptions that thrown by Java relate to fundamental errors that abuse or violate the rules of the Java language or the constraints of the Java execution environment
Manually generated exceptions are typically used to report some error condition to the caller of a method.
Keywords
Following are the five keywords that are used to manage exception in java programming. Those are explained as under
- try:
Instructions in a program that we want to monitor for exceptions are contained in a try block
- catch:
Code that can catch an exception (using catch) and can handle it is contained in catch block.
- throw:
The keyword throw is used to manually throw an exception.
- throws:
Any exception that is thrown out of a method must be specified as such by a throws clause
- finally:
Any code that absolutely must be executed before a method returns is put in a finally block
Structure of an exception
Below is the basic form of an exception-handling block:
- Try
- {
- //block of code to monitor for errors
- }
- catch(ExceptionType1 exceptionObj)
- {
- //exception handler for ExceptionType1
- }
- catch(ExceptionType2 exceptionObj)
- {
- //exception handler for ExceptionType2
- }
- //…
- Finally
- {
- //block of code to be executed before try block ends
- }
Types of Exception
In java there is a built in class i.e. Throwable, all exception types are subclasses of this Throwable class.
Throwable have two subclasses that partition exceptions into two distinct branches
Following are the two types of Exceptions
- Headed by Exception
- topped by Error
- run time exception
1. Headed by exception
This class is used for exceptional conditions that user programs should catch.
This is also the class that we will subclass to create our own custom exception types.
2. Topped by Error
It defines exceptions that are not expected to be caught under normal circumstances by our program.
Exceptions of type Error are used by the Java runtime system to indicate errors having to do with the runtime environment, itself, e.g., Stack overflow.
3. Run time exception
This is an important subclass of Exception.
Run time Exceptions are automatically defined for the programs that we write and include things such as division by zero and invalid array indexing.
Problem when we do not handle Exceptions
It is very important for user to use Exceptions in the program.
Let’s see what happens when we do not use exceptions.Example without Exceptions :
- class ExceptionByZero
- {
- {
- int d = 0;
- Int a = 42 / d;
- }
- }
When we run the above code error will come in our program, so any number divided by Zero becomes equal to infinity. So it will generate an error like:
“Unknown Exception in the main thread”
Example with Exceptions
class ExceptionTryCatch { { int a, b; try { // monitor a block of code. a = 0; b = 42 / a; } } } }
Comments
for congragulation
Add new comment
- Add new comment
- 24 views