Exception Handling:
There are 2 types of exceptions
Usually, exceptions are classified as problems that happens during execution
i. Compile time exceptions
ii. Runtime exceptions
i. Compile time Exceptions: These are 2 types:
a.Syntax errors:
Exp: String a; (Correct)
Stng a; (Incorrect)
b.Semantic errors
Variables may declared twice
Exp: int a; (Once Correct)
----
----
int a; (Twice incorrect)
NOTE: By Default these are highlighted in red in Eclipse while writing the script
ii.Runtime exceptions:
These are interruptions that happens during program execution time.
A good program should handle all the exceptions and continue with its
normal flow of program execution.
Exception Handle is a set of code that handles an exception
Exceptions can be handled in Java using "try & catch" block of code
Sytanx:{for- try & catch}
try{
statement(s)
}
catch (Exception exception_name){
statement(s)
}
NOTE: The "finally" block is executed irrespective of an exception being raised in
the try block.
It is optional to use with a try block
Syntax: {try & catch with finally}
try{
statement(s)
}
catch(Exception Exception_name){
statement(s)
}
finally{
statement(s)
}
Exp:
Scanner sc=new Scanner(System.in);
System.out.println("please enter the value:");
int b=sc.nextInt();
try{
int a=10;
int c=a/b;
System.out.println(c);
}catch(Exception e)
{
System.out.println("dont pass b value as:"+b);
//e.printStackTrace();//it notifys the exception location
}
finally{
System.out.println("SampleSelenium");
}
Exp:
try {
int a=10;
int b=0;
//int b=2; (switch between two b values to get different outcome)
int c=a/b;
System.out.println(c);
}
catch (Exception x){
System.out.println("b-value should not be zero");
System.out.print(x);
}
finally{
System.out.print("This is an Example of an exception Handling");
}
There are 2 types of exceptions
Usually, exceptions are classified as problems that happens during execution
i. Compile time exceptions
ii. Runtime exceptions
i. Compile time Exceptions: These are 2 types:
a.Syntax errors:
Exp: String a; (Correct)
Stng a; (Incorrect)
b.Semantic errors
Variables may declared twice
Exp: int a; (Once Correct)
----
----
int a; (Twice incorrect)
NOTE: By Default these are highlighted in red in Eclipse while writing the script
ii.Runtime exceptions:
These are interruptions that happens during program execution time.
A good program should handle all the exceptions and continue with its
normal flow of program execution.
Exception Handle is a set of code that handles an exception
Exceptions can be handled in Java using "try & catch" block of code
Sytanx:{for- try & catch}
try{
statement(s)
}
catch (Exception exception_name){
statement(s)
}
NOTE: The "finally" block is executed irrespective of an exception being raised in
the try block.
It is optional to use with a try block
Syntax: {try & catch with finally}
try{
statement(s)
}
catch(Exception Exception_name){
statement(s)
}
finally{
statement(s)
}
Exp:
Scanner sc=new Scanner(System.in);
System.out.println("please enter the value:");
int b=sc.nextInt();
try{
int a=10;
int c=a/b;
System.out.println(c);
}catch(Exception e)
{
System.out.println("dont pass b value as:"+b);
//e.printStackTrace();//it notifys the exception location
}
finally{
System.out.println("SampleSelenium");
}
Exp:
try {
int a=10;
int b=0;
//int b=2; (switch between two b values to get different outcome)
int c=a/b;
System.out.println(c);
}
catch (Exception x){
System.out.println("b-value should not be zero");
System.out.print(x);
}
finally{
System.out.print("This is an Example of an exception Handling");
}
No comments:
Post a Comment