EELU-OOP-Week6 - Exceptions
EELU-OOP-Week6 - Exceptions
try{
//statements that may cause an exception
}
A catch block is where you handle the
exceptions.
This block must follow the try block.
A single try block can have several catch
blocks associated with it.
When an exception occurs in try block, the
corresponding catch block that handles that
particular exception executes.
try
{
//statements that may cause an exception
}
catch (exception(type) e(object))
{
//error handling code
}
The generic exception handler can handle
all the exceptions but you should place it
at the end of catch blocks.
Output:
You should not divide a number by zero
Out of try-catch block...
class Example{
public static void main(String args[]){
try{
int arr[]=new int[7];
arr[4]=30/0; // divide by zero
System.out.println("Last Statement of try block");
}
catch(ArithmeticException e){
System.out.println("You should not divide a number by zero");
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("Accessing array elements outside of the limit");
}
catch(Exception e){
System.out.println("Some Other Exception");
}
System.out.println("Out of the try-catch block");
}
} Output:
You should not divide a number by zero
Out of the try-catch block
class Example{
public static void main(String args[]){
try{
int arr[]=new int[7];
arr[10]=10/5;
System.out.println("Last Statement of try block");
}
catch(ArithmeticException e){
System.out.println("You should not divide a number by zero");
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("Accessing array elements outside of the limit");
}
catch(Exception e){
System.out.println("Some Other Exception");
}
System.out.println("Out of the try-catch block");
}
} Output:
Accessing array elements outside of the limit
Out of the try-catch block
class Nest{
public static void main(String args[]){
//Parent try block
try{
//Child try block1
try{
System.out.println("Inside block1");
int b =45/0;
System.out.println(b);
}
catch(ArithmeticException e1){
System.out.println("Exception: e1");
}
//Child try block2
try{
System.out.println("Inside block2");
int b =45/0;
System.out.println(b);
}
catch(ArrayIndexOutOfBoundsException e2){
System.out.println("Exception: e2");
}
System.out.println("Just other statement");
}
catch(ArithmeticException e3){
System.out.println("Arithmetic Exception");
System.out.println("Inside parent try catch block");
}
catch(ArrayIndexOutOfBoundsException e4){
System.out.println("ArrayIndexOutOfBoundsException");
System.out.println("Inside parent try catch block");
}
catch(Exception e5){
System.out.println("Exception");
System.out.println("Inside parent try catch block");
}
System.out.println("Next statement..");
}
}
A finally block contains all the crucial
statements that must be executed
whether exception occurs or not.
The statements present in this block will
always execute regardless of whether
exception occurs in try block or not such
as closing a connection, stream, file, etc.
class Example
{
public static void main(String args[]) {
try{
int num=121/0;
System.out.println(num);
}
catch(ArithmeticException e){
System.out.println("Number should not be divided by zero");
}
/* Finally block will always execute
* even if there is no exception in try block
*/
finally{
System.out.println("This is finally block");
}
System.out.println("Out of try-catch-finally");
}
Output:
} Number should not be divided by zero
This is finally block
Out of try-catch-finally
class JavaFinally
{
public static void main(String args[])
{
System.out.println(JavaFinally.myMethod());
}
public static int myMethod()
{
try {
return 112;
}
finally {
System.out.println("This is Finally block");
System.out.println("Finally block ran even after return statement");
}
}
}
Output:
This is Finally block
Finally block ran even after return statement
112