EXCEPTION HANDLING IN JAVA
- Exceptions are java's runtime Errors.
- An exception is an object generated at runtime to describe a problem found during program execution.
- Exceptions can be handled using five keywords:
*try
*catch
*throw
*throws
*finally
Syntax:
try
{
// Error monitoring statements
}
Catch(Exception type exceptionobject)
{
// Exception handling block
}
Finally
{
// Final block to be executed
}
Exceptional handling mechanism :
1. Execute try blocks, if problem occurs passes
to' catch' block.
2. Catch executes & passes to finally block.
3. If no problem occurs, try-finally block is executed.
Types of Exception :
👉🏻 User defined Exception.
👉🏻 System defined Exception/Built in.
Built-in Exception:
1. Arithmetic Exception .
2. Array Index Out Of Bounds Exception.
3. Array Store Exception.
4. String Index Out Of Bounds Exception.
5. Class Not Found Exception.
6. Interrupted Exception.
Example:
Class expedemo
{
Public static void main(String as[])
{
Int(a=10,b=5,c=5,X;
try
{
X=a/(b-c);
}
Catch(Arithmetic Exception)
{
System.out.println("Divide by zero")
}
}
}