FAL (2019-20) CSE2005 ETH 302 AP2019201000577 Reference Material I Exception in Java
FAL (2019-20) CSE2005 ETH 302 AP2019201000577 Reference Material I Exception in Java
System.out.println(num);
} catch(NumberFormatException e) {
System.out.println("Number format
exception");
}
}
}
Output:
Output:
try {
file=new
FileInputStream(fileName);
x = (byte) file.read();
} catch (IOException i) {
i.printStackTrace();
return -1;} catch
(FileNotFoundException f) // Not
valid! {
f.printStackTrace();
return -1;
}
}
//Catch of Main(parent) try block
catch(Exception e3) {
//Exception Message
}
Example
class NestingDemo{
public static void main(String args[]){
//main try-block
try{
//try-block2
try{
//try-block3
try{
int arr[]= {1,2,3,4};
/* I'm trying to display the value of
* an element which doesn't exist.
The
* code should throw an exception
*/
System.out.println(arr[10]);
}catch(ArithmeticException e){
System.out.print("Arithmetic
Exception");
System.out.println(" handled in try-
block3");
}
}
catch(ArithmeticException e){
System.out.print("Arithmetic Exception");
System.out.println(" handled in try-
block2");
}
}
catch(ArithmeticException e3){
System.out.print("Arithmetic Exception");
System.out.println(" handled in main try-
block");
}
catch(ArrayIndexOutOfBoundsException e4){
System.out.print("ArrayIndexOutOfBoundsException
");
System.out.println(" handled in main try-
block");
}
catch(Exception e5){
System.out.print("Exception");
System.out.println(" handled in main try-
block");
}
}
}
Output:
and
throws ArithmeticException;
...
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:
These were the main differences between throw and throws in Java.
Throw Example
Create your own exception by extending exception class and throw your
own exception using throw clause
Output:
Throws Example
Output:
User-defined Exceptions
You can create your own exceptions in Java. Keep the
following points in mind when writing your own
exception classes −
All exceptions must be a child of Throwable.
The throws does the same thing that try-catch does but there are some
cases where you would prefer throws over try-catch. For example:
Lets say we have a method myMethod() that has statements that can
throw either ArithmeticException or NullPointerException, in this case you
can use try-catch as shown below:
But suppose you have several such methods that can cause exceptions, in
that case it would be tedious to write these try-catch for each method. The
code will become unnecessary long and will be less-readable.
One way to overcome this problem is by using throws like this: declare the
exceptions in the method signature using throws and handle the exceptions
where you are calling this method by using try-catch.
Another advantage of using this approach is that you will be forced to
handle the exception when you call this method, all the exceptions that are
declared using throws, must be handled where you are calling this method
else you will get compilation error.
public void myMethod() throws ArithmeticException,
NullPointerException
{
// Statements that might throw an exception
}
import java.io.*;
class ThrowExample {
void myMethod(int num)throws IOException,
ClassNotFoundException{
if(num==1)
throw new IOException("IOException
Occurred");
else
throw new
ClassNotFoundException("ClassNotFoundException");
}
}
Output:
Output:
ArithmeticException handled
End Of Program
class ExceptionExample{
void method()throws ArithmeticException{
throw new ArithmeticException("ArithmeticException
Occurred");
}
}
class Example1{
public static void main(String args[])throws
ArithmeticException{
ExceptionExample obj=new ExceptionExample();
obj.method();
System.out.println("End Of Program");
}
}
Output: