Vous êtes sur la page 1sur 4

Question 1: What is Exception ?

Answer:

i.Java doc says � An exception is an event, which occurs during the execution
of a program, that disrupts the normal flow of the program�s instructions.�
ii.The term �exception� means �exceptional condition� and is an occurrence that
changes the normal program flow.
iii.A bunch of things can lead to exceptions, including hardware failures,
resource exhaustion, and good old bugs.
iv.When an exception event occurs in Java , an exception is said to be
�thrown�.
v.The code that is responsible for doing something about the exception is
called an �exception handler� and it �catches� the thrown exception.
vi.Every Exception will be thrown at runtime.

Question 2: How can you handle exception in java?


Answer:
try-catch-finally blocks are used to handle exception in java.
try : This block has code which can throw exception.
catch : This block is used to handle appropriate exception.
finally : This block is used to write any clean up code irrespective of whether any
exception occurred or not.

Question 5: Can we have try without catch block in java ?


Answer:
Yes, we can have try without catch block by using finally block. You can use try
with finally. As you know finally block always executes even if you have exception
or return statement in try block except in case of System.exit()..

Question 6: What is RunTime exception in java?


Answer:
RunTime exception is the exception which is thrown at run time. These exceptions
occur due to programmatic errors and need to be corrected. Compiler is not aware of
any such exception.

Question 7: What is checked exception or compile time exception?


Answer:
Checked exceptions are those exceptions which are checked at compile. If you do not
handle them , you will get compilation error.It forces you to handle this exception
in try-catch block.

Question 8: Can you put other statements between try,catch and finally block?
Answer:
No, You can not put any statements between try, catch and finally block.

Question 9: How do you create custom exception in java?


Answer:
You just need to extend Exception class to create custom exception.If yo want to
create Unchecked Exception, you need to extend RuntimeException.
Let�s understand this with example.You have list of employee and if anyemployee�s
age in list of employee is less than 18, then you need to throw
invalidAgeException(Our custom exception).

Last updated : March 20th, 2017


No Comments
Difference between throw and throws in java
Previous
Next

In this tutorial, we are going to see difference between throw and throws in java.
throw:

throw keyword is used to throw any custom exception or predefine exception.


For example:
Let�s say you want to throw invalidAgeException when employee age is less than 18.
Create a Employee class as below.
public class Employee {

String name;
int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if(age < 18)
try {
throw new InvalidAgeException("Employee's age can not be less than 18");
} catch (InvalidAgeException e) {
e.printStackTrace();
}
this.age = age;
}
}
Create InvalidAgeException class as below
public class InvalidAgeException extends Exception{

String message;

InvalidAgeException(String message)
{
super(message);
this.message=message;
}
}
Now create a main class named EmployeeExceptionTest.java as below.
public class EmployeeExceptionTest {
public static void main(String[] args) {
Employee e1 = new Employee();
e1.setName("John");
e1.setAge(25);

Employee e2 = new Employee();


e2.setName("Martin");
e2.setAge(17);
}
}
when you run above program, you will get below output:

org.arpit.java2blog.InvalidAgeException: Employee's age can not be less than 18


at org.arpit.java2blog.Employee.setAge(Employee.java:19)
at org.arpit.java2blog.ExceptionTest.main(ExceptionTest.java:14)

Question 11: Difference between throw and throws keyword?


throw:

throw keyword is used to throw any custom exception or predefine exception.


For example:
Let�s say you want to throw invalidAgeException when employee age is less than 18.
(refer to above program)

throws:

throws keyword is used to declare list of all exception which method might throw.
It delegates responsibility of handling exception to calling method.
For example:
Let�s say you want to declare InvalidAgeException in setAge() method when employee
age is less than 18 and InvalidAgeException exception should be handled in main
method.

Create a Employee class as below.


public class Employee {

String name;
int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) throws InvalidAgeException{
if(age < 18)
throw new InvalidAgeException("Employee's age can not be less than 18");
this.age = age;
}}

Create InvalidAgeException class as below


public class InvalidAgeException extends Exception{

String message;

InvalidAgeException(String message)
{
super(message);
this.message=message;
}
}
Now create a main class named EmployeeExceptionTest.java as below.

public class EmployeeExceptionTest {


public static void main(String[] args) {
try {
Employee e1 = new Employee();
e1.setName("John");
e1.setAge(25);
Employee e2 = new Employee();
e2.setName("Martin");
e2.setAge(17);
} catch (InvalidAgeException e) {
e.printStackTrace();
}}}
when you run above program, you will get below output:

org.arpit.java2blog.InvalidAgeException: Employee's age can not be less than 18


at org.arpit.java2blog.Employee.setAge(Employee.java:19)
at org.arpit.java2blog.ExceptionTest.main(ExceptionTest.java:14)

If you notice, we have used throws keyword in Employee�s setAge method instead of
handling InvalidAgeException.

public void setAge(int age) throws InvalidAgeException{


if(age < 18)
throw new InvalidAgeException("Employee's age can not be less than 18");
this.age = age;
}

Now we have used try catch block in main method to handle InvalidAgeException.