Exception Handling inwards Java
> An Exception is an event, it occurs during execution of a program,
when normal execution of the plan is interrupted.
> Exception treatment is machinery to grip exceptions
--------------------------
Common Scenarios where exceptions may occur:
1) Scenario where ArithmeticException occurs
If nosotros split upward whatever disclose past times Zero in that place ArithmeticException occurs
Ex:
int a = 10/0;
----------------------------------------------
2) Scenario where NullPointerException occurs
if nosotros accept aught value inwards whatever variable, performing whatever functioning past times the variable,
ex:
String sec = null;
System.out.println(s.length());//NullPointerException
---------------------------------------------------
3) Scenario where NumberFormatException occurs
The incorrect formatting of whatever value, may occur NumberFormatException
Ex:
String sec = "abc";
int y = Integer.parseInt(s);
System.out.println(y);//NumberFormatException
--------------------------------------
4) Scenario where ArrayIndexOutOfBounds exception occurs
If nosotros are inserting whatever value inwards the incorrect index.
Ex:
int [] a = novel int [5];
a[10] = 100;
System.out.println(a[10]);//ArrayIndexOutOfBounds
--------------------------------------------------------------
Java Program Example:------------
int a =10;
int b = 0;
int result;
result = a/b;
System.out.println(result);
System.out.println("Hello Java");
System.out.println("Hello Selenium");
----------------------------------
Use endeavor block for Exception Handling
Syntax:
--------
try {
Statements
------
-------
------
}
catch (exception e) {
Exception treatment code
}
-----------------------------------
Java Program With Exception handling
int a =10;
int b = 0;
int result;
try{
result = a/b;
System.out.println(result);
}
catch (ArithmeticException e){
System.out.println("Divided past times Zero Error");
}
System.out.println("Hello Java");
System.out.println("Hello Selenium");
}
--------------------------------------
Multiple endeavor blocks for treatment multiple exceptions
Example:
int a =10;
int b = 0;
int result;
int [] array1 = novel int [4];
try{
result = a/b;
System.out.println(result);
}
catch (ArithmeticException e){
System.out.println("Divided past times Zero Error");
}
try{
array1[10]= 100;
System.out.println(array1[10]);
}
catch (ArrayIndexOutOfBoundsException e){
System.out.println("Array Out of Bound Error");
}
System.out.println("Hello Java");
System.out.println("Hello Selenium");
}
----------------------------------------------
Sumber http://www.gcreddy.com/
> An Exception is an event, it occurs during execution of a program,
when normal execution of the plan is interrupted.
> Exception treatment is machinery to grip exceptions
--------------------------
Common Scenarios where exceptions may occur:
1) Scenario where ArithmeticException occurs
If nosotros split upward whatever disclose past times Zero in that place ArithmeticException occurs
Ex:
int a = 10/0;
----------------------------------------------
2) Scenario where NullPointerException occurs
if nosotros accept aught value inwards whatever variable, performing whatever functioning past times the variable,
ex:
String sec = null;
System.out.println(s.length());//NullPointerException
---------------------------------------------------
3) Scenario where NumberFormatException occurs
The incorrect formatting of whatever value, may occur NumberFormatException
Ex:
String sec = "abc";
int y = Integer.parseInt(s);
System.out.println(y);//NumberFormatException
--------------------------------------
4) Scenario where ArrayIndexOutOfBounds exception occurs
If nosotros are inserting whatever value inwards the incorrect index.
Ex:
int [] a = novel int [5];
a[10] = 100;
System.out.println(a[10]);//ArrayIndexOutOfBounds
--------------------------------------------------------------
Java Program Example:------------
int a =10;
int b = 0;
int result;
result = a/b;
System.out.println(result);
System.out.println("Hello Java");
System.out.println("Hello Selenium");
----------------------------------
Use endeavor block for Exception Handling
Syntax:
--------
try {
Statements
------
-------
------
}
catch (exception e) {
Exception treatment code
}
-----------------------------------
Java Program With Exception handling
int a =10;
int b = 0;
int result;
try{
result = a/b;
System.out.println(result);
}
catch (ArithmeticException e){
System.out.println("Divided past times Zero Error");
}
System.out.println("Hello Java");
System.out.println("Hello Selenium");
}
--------------------------------------
Multiple endeavor blocks for treatment multiple exceptions
Example:
int a =10;
int b = 0;
int result;
int [] array1 = novel int [4];
try{
result = a/b;
System.out.println(result);
}
catch (ArithmeticException e){
System.out.println("Divided past times Zero Error");
}
try{
array1[10]= 100;
System.out.println(array1[10]);
}
catch (ArrayIndexOutOfBoundsException e){
System.out.println("Array Out of Bound Error");
}
System.out.println("Hello Java");
System.out.println("Hello Selenium");
}
----------------------------------------------