0% found this document useful (0 votes)
17 views

Exception Handling Module 4

Uploaded by

amogh.patadi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Exception Handling Module 4

Uploaded by

amogh.patadi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 77

MODULE-4

• Exception Handling: Fundamentals, Exception types,


Uncaught exceptions, Using try and catch block,
Multiple catch clauses, Nested try statements, throw,
throws, finally, Java’s Built-in Exceptions and
Userdefined Exceptions.
• Multi-threading: Java thread model, Main thread,
Thread life cycle, Creating a thread, Creating multiple
threads, Thread priorities, Synchronization, Inter-
thread communication, Suspending, Resuming and
Stopping threads, Using multithreading.
Exception Handling:
Exception Types
Uncaught Exceptions
Using try and catch block
Multiple catch clauses
Nested try statements
throw, throws, finally
Java’s Built-in Exceptions
User-defined Exceptions
Exception Handling
• An exception is an abnormal condition that arises in a code
sequence at run time. In other words, an exception is a run-
time error.
• When an Exception occurs the normal flow of the program
is disrupted and the program or application terminates
abnormally.
• Therefore, these exceptions are to be handled.
• Java’s exception handling brings run-time error management
into the object- oriented world.
• The Exception Handling in Java is one of the
powerful mechanism to handle the runtime errors so that the
normal flow of the application can be maintained.
• An exception can occur for many different reasons.
• Following are some scenarios where an exception
occurs.
• A user has entered an invalid data.
• A file that needs to be opened cannot be found.
• A network connection has been lost in the middle of
communications or the JVM has run out of memory.
• Some of these exceptions are caused by user error,
others by programmer error, and others by physical
resources that have failed in some manner.
Exception-Handling Fundamentals
• A Java exception is an object that describes an
exceptional (that is, error) condition that has occurred in
a piece of code.
• When an exceptional condition arises, an object
representing that exception is created and thrown in the
method that caused the error.
• That method may choose to handle the exception itself,
or pass it on.
• Either way, at some point, the exception is caught and
processed. Exceptions can be generated by the Java run-
time system, or they can be manually generated by your
code.
• Java's default exception handling mechanism
public class Test {
public static void main(String[] args) {
int a = 10;
int b = 0;
System.out.println("Before Division");
double res = a/b;
System.out.println("After Division");
}
}

Output:
Before Division
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Test.main(Test.java:8)
• When an exception occurs within a method, the method
creates (throws) an object to describe the exception. This
object is called as Exception Object, which contains the
complete information about what went wrong here, ex: type of
exception, state of program, line number in the source where
this exception occurred and so on.
• After a method throws an object, it will be caught by the
block of code written to handle it. If you have not handled an
exception in your program, the exception object will be caught
by the runtime system and it handles it in its own way. i.e.,
default exception handling will come into picture which only
does two things
 Print the error message in the console
 Terminate the program
Exception Handling Methods
• Java exception handling is managed via five keywords: try,
catch, throw, throws, and finally.
• Program statements that you want to monitor for exceptions
are contained within a try block. If an exception occurs
within the try block, it is thrown.
• Your code can catch this exception (using catch) and handle
it in some rational manner.
• System-generated exceptions are automatically thrown by
the Java run-time system.
• To manually throw an exception, use the keyword throw.
• Any exception that is thrown out of a method must be
specified as such by a throws clause.
• Any code that absolutely must be executed after a try block
completes is put in a finally block.
• This is the general form of an exception-handling block:
try {
// block of code to monitor for errors
}
catch (ExceptionType1 exOb) {
// exception handler for ExceptionType1
}
catch (ExceptionType2 exOb) {
// exception handler for ExceptionType2
}
finally {
// block of code to be executed after try block ends
}
• Here, ExceptionType is the type of exception that has
occurred.
• The try-catch block is used to handle exceptions in Java.
Here's the syntax of try...catch block:
try {
statement; // generates an exception
}
catch(Exception_type e) {
statement; // processes the exception
}
• Here, we have placed the code that might generate an
exception inside the try block. Every try block is
followed by a catch block.
• When an exception occurs, it is caught by
the catch block. The catch block cannot be used without
the try block.
class Main {
public static void main(String[] args) {
try {
// code that generate exception
int divideByZero = 5 / 0;
System.out.println("Rest of code in try block");
}
catch (ArithmeticException e) {
System.out.println("ArithmeticException => " +
e.getMessage());
}}}
Output : ArithmeticException => / by zero
• In the example, we are trying to divide a number
by 0. Here, this code generates an exception.
• To handle the exception, we have put the code, 5
/ 0 inside the try block. Now when an exception
occurs, the rest of the code inside the try block is
skipped.
• The catch block catches the exception and
statements inside the catch block is executed.
• If none of the statements in the try block
generates an exception, the catch block is
skipped.
Java finally block
• In Java, the finally block is always executed no matter
whether there is an exception or not.
• The finally block is optional. And, for each try block, there
can be only one finally block.
• The basic syntax of finally block is:
try {
//code
}
catch (ExceptionType1 e1) {
// catch block }
finally {
// finally block always executes
}
• If an exception occurs, the finally block is executed after
the try...catch block. Otherwise, it is executed after the try block.
For each try block, there can be only one finally block.
class Main {
public static void main(String[] args) {
try {
// code that generates exception
int divideByZero = 5 / 0;
}
catch (ArithmeticException e) {
System.out.println("ArithmeticException => " + e.getMessage());
}
finally { System.out.println("This is the finally block"); } } }
Output
ArithmeticException => / by zero
This is the finally block
public class Main {
public static void main(String[ ] args) {
int[] myNumbers = {1, 2, 3};
System.out.println(myNumbers[10]); // error!
}
}

• The output will be something like this:


Exception in thread "main“
java.lang.ArrayIndexOutOfBoundsException: 10
at Main.main(Main.java:4)
public class Main {
public static void main(String[] args) {
try {
int[] myNumbers = {1, 2, 3};
System.out.println(myNumbers[10]);
}
catch (Exception e) {
System.out.println("Something went wrong.");
}
finally {
System.out.println("The 'try catch' is finished."); } } }

• The output will be:


Something went wrong.
The 'try catch' is finished.
Built in Exception
catch
try
finally
throw
Throws
throws
Exception Hierarchy
• All exception and error types are subclasses of
class Throwable, which is the base class of the
hierarchy.
• Throwable, declared in java.lang, is the root of entire
exception family. It has two children, Exception and
Error classes.
• Error is used by the Java run-time system(JVM) to
indicate errors having to do with the run-time
environment itself(JRE). StackOverflowError is an
example of such an error.
 Exception class: Object of its subclasses are created
and thrown whenever unusual condition occur which
can be caught and handled it programmatically. It can
be either checked exceptions or unchecked
exceptions. Example: ArithmeticException
 Error class: Error class will be created and thrown
when a serious problem occurs beyond the application
scope which can't be controlled within an application.
Example: OutOfMemoryError will be thrown when
JVM is running short of memory.
• Checked Exception classes directly inherit the
Exception class while Unchecked Exception inherits
from a subclass of Exception called
RuntimeException.
Types of Exceptions
Java defines several types of exceptions that relate to its
various class libraries. Java also allows users to define
their own exceptions.
Exceptions can be Categorized in two ways:
1. Built-in Exceptions
a) Checked Exception
b) Unchecked Exception
2. User-Defined Exceptions
• Built-in Exceptions: Built-in exceptions are the
exceptions that are available in Java libraries. These
exceptions are suitable to explain certain error situations.
• Checked Exceptions: are also called as Compile time
Exceptions which are checked by compiler to make sure
the programmer has written the handler for these. If not, it
will throw a compile time error.
• Unchecked Exceptions: are also called as Runtime
Exceptions which are not known to the compiler at
compile time. Hence, compiler is not able to check them
for the handlers.
• In simple words, if a program throws an unchecked
exception, and even if we didn’t handle or declare it, the
program would not give a compilation error.
Uncaught Exceptions
• Before you learn how to handle exceptions in your program, it is
useful to see what happens when you don’t handle them. This
small program includes an expression that intentionally causes a
divide-by-zero error:
class Exc0 {
public static void main(String args[]) {
int d = 0;
int a = 42 / d;
}
}
• When the Java run-time system detects the attempt to divide by
zero, it constructs a new exception object and then throws this
exception.
• This causes the execution of Exc0 to stop, because once an
exception has been thrown, it must be caught by an exception
handler and dealt with immediately.
• As we haven’t supplied any exception handlers of our own,
the exception is caught by the default handler provided by
the Java run-time system. Any exception that is not caught
by your program will ultimately be processed by the default
handler. The default handler displays a string describing the
exception, prints a stack trace from the point at which the
exception occurred, and terminates the program. Here is the
exception generated when this example is executed:
• java.lang.ArithmeticException: / by zero at Exc0.main
(Exc0.java:4)
• Notice how the class name, Exc0; the method name, main;
the filename, Exc0.java; and the line number, 4, are all
included in the simple stack trace. Also, notice that the type
of exception thrown is a subclass of Exception called
ArithmeticException, which more specifically describes
what type of error happened.
Java throw keyword
•The throw statement is used together with an exception
type. There are many exception types available in Java:
ArithmeticException
FileNotFoundException
ArrayIndexOutOfBoundsException
SecurityException
• The Java throw keyword is used to explicitly throw a
single exception.
• When we throw an exception, the flow of the program
moves from the try block to the catch block.
• Throw an exception if age is below 18 (print "Access
denied"). If age is 18 or older, print "Access granted":
public class Main {
static void checkAge(int age) {
if (age < 18) {
throw new ArithmeticException("Access denied - You must be
at least 18 years old.");
}
else {
System.out.println("Access granted - You are old enough!"); }
}
public static void main(String[] args) {
checkAge(15); // Set age to 15 (which is below 18...)
}
}
• The output will be:
Exception in thread "main"
java.lang.ArithmeticException: Access denied - You
must be at least 18 years old.
at Main.checkAge(Main.java:4)
at Main.main(Main.java:12)

• If age was 20, you would not get an exception:


checkAge(20);
• The output will be:
• Access granted - You are old enough!
Java throws keyword
• The throws keyword is used to declare the type of
exceptions that might occur within the method. It is used in
the method declaration.
import java.io.*;
class Main {
// declaring the type of exception
public static void findFile() throws IOException {
// code that may generate IOException
File newFile = new File("test.txt");
FileInputStream stream = new FileInputStream(newFile);
}
public static void main(String[] args) {
try { findFile(); }
catch (IOException e) { System.out.println(e); } } }
Output
• java.io.FileNotFoundException: test.txt (The system
cannot find the file specified)
• When we run this program, if the file test.txt does not
exist, FileInputStream
throws FileNotFoundException which extends
the IOException class.
• The findFile() method specifies that an IOException can
be thrown. The main() method calls this method and
handles the exception if it is thrown.
• If a method does not handle exceptions, the type of
exceptions that may occur within it must be specified in
the throws clause.
Using try and catch
• Although the default exception handler provided by the Java run-
time system is useful for debugging, you will usually want to
handle an exception yourself.
• Doing so provides two benefits. First, it allows you to fix the
error. Second, it prevents the program from automatically
terminating. Most users would be confused (to say the least) if
your program stopped running and printed a stack trace whenever
an error occurred! Fortunately, it is quite easy to prevent this.
• To guard against and handle a run-time error, simply enclose the
code that you want to monitor inside a try block.
• Immediately following the try block, include a catch clause that
specifies the exception type that you wish to catch.
• To illustrate how easily this can be done, the following program
includes a try block and a catch clause that processes the
ArithmeticException generated by the division-by-zero error:
class Exc2 {
public static void main(String args[]) {
int d, a;
try { // monitor a block of code.
d = 0;
a = 42 / d;
System.out.println("This will not be printed.");
}
catch (ArithmeticException e) { // catch divide-by-zero error
System.out.println("Division by zero.");
}
System.out.println("After catch statement.");
}
}
• This program generates the following output:
Division by zero. After catch statement.
Multiple catch Clauses
• To handle more than one exception which could be raised by
a single piece of code, you can specify two or more catch
clauses, each catching a different type of exception.
• When an exception is thrown, each catch statement is
inspected in order, and the first one whose type matches that
of the exception is executed.
• After one catch statement executes, the others are bypassed,
and execution continues after the try/catch block.
• The following example traps two different exception types:
// Demonstrate multiple catch statements.
class MultiCatch {
public static void main(String args[]) {
try {
int a = args.length;
System.out.println("a = " + a);
int b = 42 / a;
int c[] = { 1 };
c[42] = 99;
}
catch(ArithmeticException e) {
System.out.println("Divide by 0: " + e);
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Array index oob: " + e);
}
System.out.println("After try/catch blocks.");
}
}
• This program will cause a division-by-zero exception if it is
started with no command-line arguments, since a will equal
zero. It will survive the division if you provide a command-
line argument, setting a to something larger than zero.
• But it will cause an ArrayIndexOutOfBoundsException,
since the int array c has a length of 1, yet the program
attempts to assign a value to c[42].
• Here is the output generated by running it both ways:
C:\>java MultiCatch a = 0
Divide by 0: java.lang.ArithmeticException: / by zero After
try/catch blocks.
C:\>java MultiCatch TestArg a = 1
Array index oob:
java.lang.ArrayIndexOutOfBoundsException:42 After
try/catch blocks.
Nested try Statements
• The try statement can be nested. That is, a try statement
can be inside the block of another try.
• Each time 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.
• This continues until one of the catch statements
succeeds, or until all of the nested try statements are
exhausted.
• If no catch statement matches, then the Java run-time
system will handle the exception.
// An example of nested try statements.
class NestTry {
public static void main(String args[]) {
try {
int a = args.length;
/* If no command-line args are present, the following statement will
generate a divide-by-zero exception. */
int b = 42 / a;
System.out.println("a = " + a);
try { // nested try block
/* If one command-line arg is used, then a divide-by-zero exception
will be generated by the following code. */
if(a==1) a = a/(a-a); // division by zero
/* If two command-line args are used, then generate an out-of-
bounds exception. */
if(a==2) {
int c[] = { 1 };
c[42] = 99; // generate an out-of-bounds exception
} }
catch(ArrayIndexOutOfBoundsException e) { System.out.println("Array
index out-of-bounds: " + e);
} }
catch(ArithmeticException e) { System.out.println("Divide by 0: " + e);
} }}
• This program nests one try block within another.
• When you execute the program with no command-line arguments, a
divide-by-zero exception is generated by the outer try block.
• Execution of the program with one command-line argument generates a
divide-by-zero exception from within the nested try block.
• Since the inner block does not catch this exception, it is passed on to
the outer try block, where it is handled.
• If you execute the program with two command-line arguments, an
array boundary exception is generated from within the inner try block.
Here are sample runs that illustrate each case:
C:\>java NestTry
Divide by 0: java.lang.ArithmeticException: / by zero
C:\>java NestTry One a = 1
Divide by 0: java.lang.ArithmeticException: / by zero
C:\>java NestTry One Two a = 2
Array index out-of-bounds:
java.lang.ArrayIndexOutOfBoundsException:42
Java’s Built-in Exceptions
• Inside the standard package java.lang, Java defines
several exception classes.
• The most general of these exceptions are subclasses of
the standard type RuntimeException.
• These exceptions need not be included in any method’s
throws list.
• These are called unchecked exceptions because the
compiler does not check to see if a method handles or
throws these exceptions.
• The unchecked exceptions defined in java.lang are
listed in Table 10-1.
User-Defined Exception
User-Defined Exception
• An exception is an event, which occurs during the
execution of a program, that disrupts the normal flow of
the program’s instructions.
• In programming, exceptions are used to handle errors
and other unusual events.
• They are typically generated when a runtime error
occurs, such as a division by zero or array access that is
out of bounds.
• The exception can be caught and handled in the code by
an exception handler, allowing the program to continue
running instead of crashing or producing an incorrect
result.
• User defined exception in java is a powerful tool for
handling specific requirements and scenarios in Java.
• They allow programmers to provide more descriptive
error messages and manage exceptions in a better and
more organized manner.
• User-defined exceptions are custom exceptions that a
programmer can create in their code to handle specific
error conditions or situations.
• These exceptions are derived from the base exception
class and provide a way for the programmer to tailor the
exception-handling process to their specific needs.
• User-defined exceptions are used to indicate errors that
are specific to the particular application being developed
and are often used to provide a higher level of
abstraction and readability in the code.
• For example, a programmer may create a custom
exception for a bank account class that checks if a
withdrawal would result in a negative balance.
• If a withdrawal would cause a negative balance, the
custom exception is thrown and can be caught and
handled by the code.
• This allows the programmer to provide more meaningful
error messages and handling of specific error conditions
in the code, rather than relying on generic exceptions.
Why use User-defined Exceptions?
• User defined exception in java are used to provide a
higher level of abstraction and readability in the code by
encapsulating error-handling logic in specific parts of
the code. They offer several benefits, such as:
• Improved readability and maintainability: User-
defined exceptions make it easier to understand the code
and improve its maintainability by providing meaningful
error messages and handling specific error conditions.
• Better error handling: Custom exceptions can be used
to handle specific errors in a more meaningful and
appropriate manner, leading to better error handling and
improved reliability of the code.
• Modularity: User-defined exceptions can be used to
create modular code, making it easier to manage
complex and large applications.
• Reusability: Custom exceptions can be reused across
different parts of the code, making it easier to implement
error handling in a consistent manner.
• Better error reporting: User-defined exceptions can
provide more information about the error, making it
easier to diagnose and fix the problem.
• To implement a user-defined exception in Java, follow
these steps:
• Create a custom exception class that extends the base
exception class (java.lang.Exception).
• Define the constructor for the custom exception class.
The constructor can accept parameters to provide more
information about the error.
• Override the toString() method to provide a custom error
message.
• Use the "throw" keyword to throw an instance of the
custom exception when the error condition occurs.
• Use a "try-catch" block to catch the exception and
handle it appropriately.
/* Program that throws a 'NoMatchException' when a
string is not equal to “India” */
class NoMatchException extends Exception
{
private String str;
NoMatchException(String str1)
{
str=str1;
}
public String toString()
{
return "NoMatchException --> String is not India and
string is "+str;
}
}
class P31
{
public static void main(String args[ ])
{
String str1= new String("India");
String str2= new String("Pakistan");
try
{
if(str1.equals("India"))
System.out.println(" String is : "+str1);
else
throw new NoMatchException(str1);
if(str2.equals("India"))
System.out.println("\n String is :
"+str2);
else
throw new
NoMatchException(str2);
}
catch(NoMatchException e)
{
System.out.println("\nCaught ...."+e);
}
}
}
• Here is an example implementation of a user-defined
exception class:
class NegativeBalanceException extends Exception {
public NegativeBalanceException(String message) {
super(message);
}
}
class BankAccount {
private double balance;
public BankAccount(double balance) {
this.balance = balance;
}
public double getBalance(){
return balance;
}
public void withdraw(double amount) throws
NegativeBalanceException {
if (balance - amount < 0) {
throw new NegativeBalanceException("Insufficient funds
to withdraw " + amount + " dollars.");
}
else {
balance -= amount;
}
}
}
class Main {
public static void main(String[] args) {
BankAccount ba = new BankAccount(100);
try {
ba.withdraw(200);
}
catch (NegativeBalanceException ex) {
System.out.println(ex.getMessage());
}
System.out.println("Remaining balance: " + ba.getBalance());
}
}
• Output:
Insufficient funds to withdraw 200.0 dollars.
Remaining balance: 100.0
Explanation:
• In this example, we’ve created a custom exception class
NegativeBalanceException that extends Exception and a
class BankAccount that has a withdraw method.
• If the balance is less than the amount being withdrawn,
an instance of NegativeBalanceException is thrown with
an error message.
• The exception is then caught in the main method and its
error message is printed.
User defined Exception
Custom Exception

You might also like