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

Handling Expt .Discussion 1

The document explains exception handling in Java, focusing on the use of try, catch, throw, and finally blocks to manage errors gracefully. It provides syntax and practical examples for each concept, illustrating how exceptions are caught and handled in code. Additionally, it emphasizes the importance of the finally block, which executes regardless of whether an exception occurs.

Uploaded by

robert
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)
2 views5 pages

Handling Expt .Discussion 1

The document explains exception handling in Java, focusing on the use of try, catch, throw, and finally blocks to manage errors gracefully. It provides syntax and practical examples for each concept, illustrating how exceptions are caught and handled in code. Additionally, it emphasizes the importance of the finally block, which executes regardless of whether an exception occurs.

Uploaded by

robert
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 handling is one of the major aspects whereby developers can

handle errors or exceptional conditions with grace. To explain concepts on


try, catch, throw, and finally blocks, practical examples will be used to
explain these abstract concepts.

1. try
 A try block consists of all the doubtful statements that can throw
exceptions.
 A try block cannot be executed on itself; it requires at least
one catch block or finally block.
 If an exception occurs, the control flows from the try-to-
catch block.
 When an exception occurs in a try block, the appropriate
exception object is redirected to the catch block.
This catch block handles the exception according to its
statements and continues the execution.

Syntax

try
{
//Doubtful Statements.
}

2. catch
 The catch block handles the exception raised in the try block.
 The catch block or blocks follow every try block.
 The catch block catches the thrown exception as its parameter
and executes the statements inside it.
 The declared exception must be the parent class exception,
the generated exception type in the exception class hierarchy,
or a user-defined exception.

Syntax

try
{
// code
}
catch(Exception e)
{
// code to handle exceptions
}

Examples Illustrating Implementation of try-catch


blocks for Java Exception Handling
try-catch block

1.
2. class Main
3. {
4. public static void main(String[] args)
5. {
6. try
7. {
8. // code that generate exception
9. int divideByZero = 7 / 0;
10. System.out.println("Rest of code in try block");
11. }
12. catch (ArithmeticException e) {
13. System.out.println("ArithmeticException => " + e.getMessage());
14. }
15. }
}

Run Code >>

In the above code, we have put the "int divideByZero=7/0" in


the try block because this statement must not be executed if the
denominator is 0. If the denominator is 0, the statements after
this statement in the try block are skipped. The catch block
catches the thrown exception as its parameter and executes the
statements inside it.

Output
ArithmeticException => / by zero

3. throw
 The throw keyword is used to explicitly throw a checked or
an unchecked exception.
 The exception that is thrown needs to be of type Throwable or a
subclass of Throwable.
 We can also define our own set of conditions for which we can
throw an exception explicitly using the throw keyword.
 The program's execution flow stops immediately after the throw
statement is executed, and the nearest try block is checked to
see if it has a catch statement that matches the type of
exception.

Syntax

throw new exception_class("error message");

Example of Exception Handling using Java throw

class ThrowExample {
// Method to check if a number is negative
public static void checkNumber(int number) {
if (number < 0) {
// Throwing an IllegalArgumentException if the number is negative
throw new IllegalArgumentException("Number cannot be negative");
} else {
System.out.println("Number is " + number);
}
}

public static void main(String[] args) {


try {
// Trying to check a negative number
checkNumber(-5);
} catch (IllegalArgumentException e) {
// Handling the thrown exception
System.out.println("Caught an exception: " + e.getMessage());
}
}
}
Run Code >>
In the main method, the exception is captured and handled using
a try-catch block, showing the exception message if the input is
negative. This Java class, ThrowExample, contains a method
checkNumber that throws an IllegalArgumentException if the input
number is negative.

Output
Caught an exception: Number cannot be negative
4. finally
The finally block in Java always executes even if there are
no exceptions. This is an optional block. It is used to execute
important statements such as closing statements, releasing resources,
and releasing memory. There could be one final block for every try
block. This finally block executes after the try...catch block.

Syntax

try
{
//code
}
catch (ExceptionType1 e1)
{
// catch block
}
finally
{
// finally block always executes
}

Example of Java Exception Handling using 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");
}
}
}
Run Code >>
In this Java example, trying to divide by zero results in
an ArithmeticException that is caught and accompanied by an error
message. The "finally" block also always runs, printing "This is the
finally block" whether or not an exception was raised.

Output
ArithmeticException => / by zero
This is the finally block

References

Eck, D.J. (2022). Introduction to programing using java version 9, Fx edition

https://www.scholarhat.com/tutorial/java/exception-handling-in-java

You might also like