CO_IF_22412_UO2
Sushama Pawar, Lecturer, Vidyalankar Polytechnic
Date: 04 February 2021
MSBTE LEAD: Learning at your Doorstep
Unit 04 :
Exception Handling and
Multithreading
Written by
Sushama Pawar
Lecturer, Department of Information Technology[NBA Accredited],Vidyalankar Polytechnic,
Mumbai
Unit Outcome 2 :
Develop program for handling the
given exception.
Written by
Sushama Pawar
Lecturer, Department of Information Technology[NBA Accredited],Vidyalankar Polytechnic,
Mumbai
Learning Outcome 2a : Students
should understand concept of try,
catch, nested try, throws and finally
statement..
Written by
Sushama Pawar
Lecturer, Department of Information Technology[NBA Accredited],Vidyalankar Polytechnic,
Mumbai
What we will learn today
1. Concept of try-catch and finally block Key takeaways
2. What is nested try? Concept of try-catch-finally, nested try and throws
. What is throw and throws?
Sushama Pawar
Lecturer, Department of Information Technology[NBA Accredited],Vidyalankar Polytechnic,
Mumbai
Page 5 Maharashtra State Board of Technical Education 2 Feb 2021
Concept Map
Try-catch-finally
Nested try
Throw
throws
Page 6 Maharashtra State Board of Technical Education 2 Feb 2021
Learning Objective/ Key learning
► Understand concept of try-catch and finally block.
► Develop program using try-catch and finally block.
► Understand concept of nested try with example
► Understand concept of throw and throws
► Distinguish between throw and throws
Page 7 Maharashtra State Board of Technical Education 2 Feb 2021
Concept Explanation: try, catch and finally block
► An exception is an event, which occurs during the execution of a program, that stop the flow of the
program's instructions and takes appropriate actions if handled.
► Exceptional handling mechanism provides a means to detect errors and throw exceptions, and then
to catch exceptions by taking appropriate actions. Java Exception handles as follow
Find the problem (Hit the exception)
Inform that an error has occurred ( throw the Exception)
Receive the error information(Catch the exception)
Take corrective action ( Handle the Exception)
► Exceptional handling in java by five keywords are as follows:
1. try
2. catch
3. finally
4. throw
5. throws
8
Page 8 Maharashtra State Board of Technical Education 2 Feb 2021
Concept of try block
try:
► This block applies a monitor on the statements written inside it.
► If there exist any exception, the control is transferred to catch or finally block.
Syntax:
try
{
// block of code to monitor for errors
}
Page 9 Maharashtra State Board of Technical Education 2 Feb 2021
Concept of catch block
Catch:
This block includes the actions to be taken if a particular exception occurs.
Syntax:
catch (ExceptionType1 exOb)
{
// exception handler for ExceptionType1
}
10
Page 10 Maharashtra State Board of Technical Education 2 Feb 2021
Example of try and catch
Page 11 Maharashtra State Board of Technical Education 2 Feb 2021
Example of Multiple catch block
It is possible to have multiple catch block in our program
Page 12 Maharashtra State Board of Technical Education 2 Feb 2021
Concept of Nested Try
► The nested try is used to implement multiple try statements in a single block of main method.
Syntax: -
Try
{
-------
Try
{
-------
}
}
► The try statement can be nested. That is, a try statement can be used inside the block of another try.
► Each time when a try statement is entered, the context of that exception is pushed on the stack. If an
inner try statement does not have a catch handler for a particular exception, the stack is unwound
and the next try statement’s catch handlers are inspected for a match of exception.
Page 13 Maharashtra State Board of Technical Education 2 Feb 2021
Cont…
► This continues until one of the catch statements succeeds, or until all the nested try statements are
exhausted.
► If no catch statement matches, then the Java run-time system will handle the exception
Page 14 Maharashtra State Board of Technical Education 2 Feb 2021
Example of Nested Try
// Nested try blocks {
import java.util.Scanner; System.out.println("Array indexing wrong");
class NestTry }
{ System.out.println("Outer try end...");
public static void main(String args[]) }
{ catch(ArithmeticException e) //2
int x,z; {
Scanner in = new Scanner(System.in); System.out.println("Division by zero");
System.out.print("Enter number : "); }
x = in.nextInt(); System.out.println("Program end...");
try }
{ }
z = 82 / x; //statement1
System.out.println("Division: "+z);
try
{
int a = 100 / (x-1); //statement2
short arr[] = {15};
arr[10] = 25; //statement3
System.out.println("Inner try end...");
}
catch(ArrayIndexOutOfBoundsException e)
Page 15 Maharashtra State Board of Technical Education 2 Feb 2021
Concept of finally block
finally:
► finally block includes the statements which are to be executed in any case, in case the exception is
raised or not
Syntax:
finally
{
// block of code to be executed before try block ends
}
16
Page 16 Maharashtra State Board of Technical Education 2 Feb 2021
Example of finally
class FinallyClause
{
public static void main(String args[])
{
try
{
//int val = 0; //statement1
//int m = 100 / val; //statement2
int []x = new int[-5]; //statement3
System.out.println("No output");
}
catch(ArithmeticException e) Now, if we removed the comments from statement1 and
{ statement2, the output will be:
System.out.println("Exception: "+e);
}
finally
{
System.out.println("Program end");
System.out.println("Bye bye...");
}
}
}
Page 17 Maharashtra State Board of Technical Education 2 Feb 2021
Concept of throw
throw:
► This keyword is generally used in case of user defined exception, to forcefully raise the exception and
take the required action.
Syntax:
throw throwable instance;
18
Page 18 Maharashtra State Board of Technical Education 2 Feb 2021
Demonstration of throw clause
// Demonstration of throw clause Output:
class ThrowDemo Exception caught
{ Addition: 30
public static void main(String args[])
{
int x = 10, y = 20;
int z;
z = x + y;
try
{
throw new ArithmeticException();
}
catch(Exception e)
{
System.out.println("Exception caught");
System.out.println("Addition: "+z);
}
}
}
Page 19 Maharashtra State Board of Technical Education 2 Feb 2021
Concept of throws
throws :
► throws keyword can be used along with the method definition to name the list of exceptions which
are likely to happen during the execution of that method.
► In that case , try … catch block is not necessary in the code.
Syntax:
Type method-name (parameter list) throws exception list
{
// body of method
20
}
Page 20 Maharashtra State Board of Technical Education 2 Feb 2021
Distinguish between throw and throws
Sr. No. throw throws
1 Whenever we want to force an exception then when we know that a particular exception may be
we use throw keyword. thrown or to pass a possible exception then we use
throws keyword.
2 "Throw" is used to handle user-defined JVM handles the exceptions which are specified by
exception. "throws".
3 It can also pass a custom message to your Point to note here is that the Java compiler very well
exceptionhandling module. knows about the exceptions thrown by some
methods
so it insists us to handle them.
4 Throw keyword can also be used to pass a We can also use throws clause on the surrounding
custom message to the exception handling method instead of try and catch exception handler.
module i.e. the message which we want to be
printed.
5 Throw is used to through exception system Throws is used for to throws exception
explicitly. means throws ioexception and
servletexception and etc.
6 Throw is used to actually throw the Whereas throws is declarative for the
exception. method. They are not interchangeable.
7 It is used to generate an exception. It is used to forward an exception.
Page 21 Maharashtra State Board of Technical Education 2 Feb 2021