Exception Handling Example Java Program
Definition
Exception handling is the process of responding to the occurrence, during computation, of exceptions ? anomalous or exceptional conditions requiring special processing ? often changing the normal flow of program execution. It is provided by specialized programming language constructs or computer hardware mechanisms.Syntax
try{ //Statements }catch(Expression_type Variable_name){ //Statements }
Exception Handling Example Program
class ExceptionHandlingExample{ public static void main(String[] args){ int num1,num2,num3; num1=20; num2=0; try{ num3=num1/num2; System.out.println("Result is "+num3); }catch(ArithmeticException ae){ System.out.println("Numbers cannot be divided by zero"); } num3=num1+num2; System.out.println("Result after addition is "+num3); } }
Sample Output
Output is: Numbers cannot be divided by zero Result after addition is 20
Exception Handling Programs
- Exception Handling Example Java Program
- Nested Try Example Java Program
- Throw clause Java Example Program
- Throws clause Java Example Program
- Finally Block Java Example Program
- Try-Catch-Finally Java Example Program
- Pass Argument while throwing Exception Java Example Program
- Multiple Catch Blocks Java Example Program
- Re-Throw Exception Java Example Program
- Print stack trace of the Exception Java Example Program
- Handle Exception without Catch block Java Example Program
- Custom Exception Java Example program
- Termination of Program Due To Unhandled Exception Java Example Program
- Divide by Zero Java Example Program
- Null Pointer Exception Java Example Program
- ArrayIndexOutOfBounds Exception Java Example Program
- Number Format Exception Java Example Program
- String Index Out Of Bounds Exception Java Example Program
- ParseException Java Example Program
Read More Articles
- Multiple Inheritance Using Interface Example Java Program
- Single Inheritance Example Java Program
- Multilevel Inheritance Example Java Program
- Hierarchical Inheritance Example Java Program
- Find all Substrings of a given string Example Java Program
- Create Matrix Example Java Program
- Sum Of Three Numbers Example Java Program
- Heap Sort Example Java Program
- Twin Prime Example Java Program
- Compile Time Polymorphism Example Java Program