0% found this document useful (0 votes)
13 views5 pages

Exception Handle

Uploaded by

kewatkuldeep558
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views5 pages

Exception Handle

Uploaded by

kewatkuldeep558
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Exception Handle

Showing Error is NOT Exception Handle, Java is a


Robust Language which Auto Handle Compile Time and
Run Time Errors.. Compile Time errors are syntax errors
where as Run time Errors Occur Due to Certain
Unexpected Conditions like Wrong Input, No Server
Connection etc.. Handling these Expected Run Time
Errors are done through java.lang.Exception class.

Exception class is Supermost class and All Exception


classes like IOException, NullPointerException,
ArrayIndexOutofBounds, ArithmeticException class etc
are derived from java.lang.Exception class (We can
create our Own Exception class by extending Exception
class)
Note :
 instead of handleing multiple Exceptions we can
use single java.lang.Exception class to handle All
Excepted Errors.

 If a method throws some exception and we are


using that method in another method it is
compulsory to throw same exception else it will give
compile error.
We can handle Exception by :

-> throws – throws is a Key word used with method


declaration to throw expected error when error occur on
calling a method .

public void calculate(int i,int j)throws ArithmeticException


{
System.out.println(i*j);
System.out.println(i+j);
System.out.println(i/j);
System.out.println(i-j);

Obj.calculate(12,0); // error cannot divide by zero

-> try , catch, finally block


try{ } block is a block where we put code where we
expect run time error.

-> try cannot be without catch, catch block is called


when error occur
catch(Exception e) {
e.printStackTrace();
// alternate code
}
-> finally block is always called wether there is error or
not , hence finally is always executed and we put that
code in finally which we know will execute always.

Throw vs Throws in java


1. Throws clause is used to declare an exception, which means it works
similar to the try-catch block. On the other hand throw keyword is used to
throw an exception explicitly.

2. If we see syntax wise than throw is followed by an instance of Exception


class and throws is followed by exception class names.
For example:

throw new ArithmeticException("Arithmetic Exception");


and

throws ArithmeticException;
3. Throw keyword is used in the method body to throw an exception, while
throws is used in method signature to declare the exceptions that can occur in
the statements present in the method.

For example:
Throw:

...
void myMethod() {
try {
//throwing arithmetic exception using throw
throw new ArithmeticException("Something went wrong!!");
}
catch (Exception exp) {
System.out.println("Error: "+exp.getMessage());
}
}
...
Throws:

...
//Declaring arithmetic exception using throws
void sample() throws ArithmeticException{
//Statements
}
...
4. You can throw one exception at a time but you can handle multiple
exceptions by declaring them using throws keyword.
For example:
Throw:

void myMethod() {
//Throwing single exception using throw
throw new ArithmeticException("An integer should not be divided by zero!!");
}
..
Throws:

//Declaring multiple exceptions using throws


void myMethod() throws ArithmeticException, NullPointerException{
//Statements where exception might occur
}
These were the main differences between throw and throws in Java. Lets
see complete examples of throw and throws keywords.

Throw Example
To understand this example you should know what is throw keyword and how
it works, refer this guide: throw keyword in java.

public class Example1{


void checkAge(int age){
if(age<18)
throw new ArithmeticException("Not Eligible for voting");
else
System.out.println("Eligible for voting");
}
public static void main(String args[]){
Example1 obj = new Example1();
obj.checkAge(13);
System.out.println("End Of Program");
}
}
Output:
Exception in thread "main" java.lang.ArithmeticException:
Not Eligible for voting
at Example1.checkAge(Example1.java:4)
at Example1.main(Example1.java:10)

Throws Example
To understand this example you should know what is throws clause and how it
is used in method declaration for exception handling, refer this guide: throws
in java.

public class Example1{


int division(int a, int b) throws ArithmeticException{
int t = a/b;
return t;
}
public static void main(String args[]){
Example1 obj = new Example1();
try{
System.out.println(obj.division(15,0));
}
catch(ArithmeticException e){
System.out.println("You shouldn't divide number by zero");
}
}
}
Output:

You shouldn't divide number by zero

You might also like