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

Java p11

The document discusses exception handling in Java. It provides examples of using try/catch blocks to handle exceptions, finally blocks for cleanup, and throw/throws clauses. It includes code samples showing a normal program with division by zero, handling the exception with try/catch, using multiple catch blocks, finally blocks, and throw/throws clauses. The conclusion emphasizes the importance of exception handling for robust Java programs.

Uploaded by

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

Java p11

The document discusses exception handling in Java. It provides examples of using try/catch blocks to handle exceptions, finally blocks for cleanup, and throw/throws clauses. It includes code samples showing a normal program with division by zero, handling the exception with try/catch, using multiple catch blocks, finally blocks, and throw/throws clauses. The conclusion emphasizes the importance of exception handling for robust Java programs.

Uploaded by

Khan.ali
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Practical 11:- Create a program in Java to implement Exception

Handling. And also perform the following:-


1) Normal Program in Java with one try and two catches.
2) To show the concept of finally
3) WAP for throw and throws clause.

1. INTRODUCTION

Exception handling in Java is a mechanism that allows programmers to handle and


manage errors or exceptional conditions that may occur during the execution of a
program. These exceptional conditions are typically situations that disrupt the normal
flow of a program and can lead to runtime errors.
In Java, exceptions are objects that represent these exceptional conditions, and the
process of dealing with exceptions is called exception handling. Exception handling in
Java is implemented using a combination of keywords, such as try, catch, throw, throws,
and finally, along with exception classes. Here's a basic overview of how exception
handling works in Java:

1. Throwing an Exception: When an exceptional condition occurs in your code, you


can create an exception object using the throw keyword. For example:
throw new SomeException("This is an exceptional condition.");
2. Catching Exceptions: To handle exceptions, you use the try and catch blocks. The try
block encloses the code where an exception might occur, and the catch block contains
code to handle the exception if it occurs. You can have multiple catch blocks to handle
different types of exceptions. For example:
try {
// Code that might throw an exception
} catch (SomeException e) {
// Handle the SomeException
} catch (AnotherException e) {
// Handle the AnotherException
}
3. Finally Block: You can also use a finally block after try and catch. The code in the
finally block is executed whether an exception occurs or not. This is useful for clean-up
operations. For example:
try {
// Code that might throw an exception
} catch (SomeException e) {
// Handle the SomeException
} finally {
// Cleanup code
}
4. Propagating Exceptions: If you don't want to handle an exception locally, you can
declare it using the throws clause in the method signature. This means the method will
propagate the exception to its caller, and the caller can handle it or propagate it further.
public void someMethod()
throws SomeException {
// Code that might throw SomeException
}

//Java program to implement division by 0.

public class DivisionByZeroExample {


public static void main(String[] args) {
int a = 10;
int b = 0;
int result = a / b; // Division by zero
System.out.println("Result: " + result);
System.out.println("This line will not be executed due to the unhandled
exception.");
}
}
//Java program to implement division by zero using try & catch

public class DivisionByZeroWithExceptionHandling {


public static void main(String[] args) {
int a = 10;
int b = 0;
try {
int result = a / b; // Division by zero
System.out.println("Result: " + result); // This line will not be executed
} catch (ArithmeticException e) {
System.out.println("Exception caught: Division by zero is not allowed.");
}
System.out.println("Program continues after exception handling.");
}
}

//Java program with one try and two catch block.

public class DivisionByZeroWithMultipleCatches {


public static void main(String[] args) {
int a = 10;
int b = 0;
try {
int result = a / b; // Division by zero
System.out.println("Result: " + result); // This line will not be executed
} catch (ArithmeticException e) {
System.out.println("ArithmeticException caught: Division by zero is not
allowed.");
} catch (Exception e) {
System.out.println("Exception caught: An error occurred.");
}
System.out.println("Program continues after exception handling.");
}
}

//Java program to show the concept of finally.


public class FinallyDivisionByZeroExample {
public static void main(String[] args) {
int a = 10;
int b = 0;
try {
int result = a / b; // Division by zero
System.out.println("Result: " + result); // This line will not be executed
} catch (ArithmeticException e) {
System.out.println("ArithmeticException caught: Division by zero is not
allowed.");
} finally {
System.out.println("Finally block executed.");
}
System.out.println("Program continues after exception handling.");
}
}

//Java program to implement throw clause


import java.util.Scanner;
class Test {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Please enter your roll number");
int roll = s.nextInt();

try {
if (roll < 0) {
throw new ArithmeticException("The number entered is not positive");
} else {
System.out.println("Valid roll number");
}
} catch (ArithmeticException e) {
System.out.println("An exception is thrown");
System.out.println(e.getMessage());
}
}
}

//Java program to implement throws clause


class Test {

public static void calculate()


throws ArithmeticException, ArrayIndexOutOfBoundsException {
int num = 10 / 0;
// some code that might throw ArrayIndexOutOfBoundsException
}
public static void main(String[] args) {
try {
calculate();
} catch (ArithmeticException e) {
System.out.println("Arithmetic Exception thrown");
System.out.println(e.getMessage());
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("ArrayIndexOutOfBounds Exception thrown");
System.out.println(e.getMessage());
}
}

Conclusion
Exception handling in Java is a critical mechanism for dealing with errors and
exceptional situations during program execution. Key aspects include the use of `try` and
`catch` blocks to handle exceptions, the optional `finally` block for cleanup operations,
and the ability to throw and propagate exceptions. Exception types range from built-in
exceptions to custom ones, and handling them contributes to program robustness and
error resilience, ensuring that applications can gracefully handle unexpected situations.
Effective exception handling is crucial for writing reliable Java software.

You might also like