WebService_PDF download by click on the link
Monday, 17 April 2017
Saturday, 7 January 2017
Exception Handling interview questions
1. Create custom exception
class MyOwnException extends Exception {
public MyOwnException(String msg){
super(msg);
}
}
class CustomExceptionTest {
static void employeeAge(int age) throws MyOwnException{
if(age < 0)
throw new MyOwnException("Age can't be less than zero");
else
System.out.println("Input is valid!!");
}
public static void main(String[] args) {
try {
employeeAge(-2);
}
catch (MyOwnException e) {
e.printStackTrace();
}
}
}
--------------------------------------------------------------------------------
MyOwnException: Age can't be less than zero
at EmployeeTest.employeeAge(EmployeeTest.java:10)
at EmployeeTest.main(EmployeeTest.java:16)
//Finally block overrides the value returned by try and catch blocks.
public class returnStatement {
public static String returnMethod(){
try {
return "try";
} catch (Exception e) {
return "catch";
}finally{
return "finally";
}
}
public static void main(String[] args) {
System.out.println(returnStatement.returnMethod());
}
}
-------------------------------------------------------------------------
finally
-------------------------------------------------------------------------
class MyOwnException extends Exception {
public MyOwnException(String msg){
super(msg);
}
}
class CustomExceptionTest {
static void employeeAge(int age) throws MyOwnException{
if(age < 0)
throw new MyOwnException("Age can't be less than zero");
else
System.out.println("Input is valid!!");
}
public static void main(String[] args) {
try {
employeeAge(-2);
}
catch (MyOwnException e) {
e.printStackTrace();
}
}
}
--------------------------------------------------------------------------------
MyOwnException: Age can't be less than zero
at EmployeeTest.employeeAge(EmployeeTest.java:10)
at EmployeeTest.main(EmployeeTest.java:16)
--------------------------------------------------------------------------------
2. Return statement//Finally block overrides the value returned by try and catch blocks.
public class returnStatement {
public static String returnMethod(){
try {
return "try";
} catch (Exception e) {
return "catch";
}finally{
return "finally";
}
}
public static void main(String[] args) {
System.out.println(returnStatement.returnMethod());
}
}
-------------------------------------------------------------------------
finally
-------------------------------------------------------------------------
3. When the finally block doesn't execute, following are the circumstances
1.When we place System.exit() in our code for stopping the jvm.
2.Abnormal situations like H/w problems.
3. finally block raises exception.
1.When we place System.exit() in our code for stopping the jvm.
2.Abnormal situations like H/w problems.
3. finally block raises exception.
Subscribe to:
Posts (Atom)