Advantages of Exception Handling - onlinetutorialspoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
Java provides a sophisticated exception handling mechanism that enables you to detect exceptional conditions in your programs and fix the exceptions as and when they occur. Using exception handling features offers several advantages. Let’s examine these advantages in detail.
One of the important purposes of exception handling in Java is to continue program execution after an exception is caught and handled. 
The execution of a Java program does not terminate when an exception occurs. Once the exception is resolved, program execution continues till completion. 
By using well-structured try, catch, and finally blocks, you can create programs that fix exceptions and continue execution as if there were no errors. If there is a possibility of more than one exception, you can use multiple catch blocks to handle the different exceptions.
The following program generates two random integers in each iteration of the for loop and performs a division operation. If a division by zero error occurs, the exception is handled in the catch block. Thus, an arithmetic exception does not terminate the program and the for loop continues execution after the catch block is executed.
import java.util.Random;
 
class ExceptionExample {
 
    public static void main(String args[]) {
        int a = 0, b = 0, c = 0;
        Random r = new Random();
 
        for (int i = 0; i < 10; i++) {
            try {
                b = r.nextInt();
                c = r.nextInt();
                a = 12345 / (b / c);
            } catch (ArithmeticException e) {
                System.out.println("Division by zero");
                a = 0;
            }
            System.out.println("a: " + a);
        }
    }
}
The use of try/catch blocks segregates error-handling code and program code making it easier to identify the logical flow of a program. The logic in the program code does not include details of the actions to be performed when an exception occurs. Such details are present in the catch blocks.
Unlike many traditional programming languages that include confusing error reporting and error handling code in between the program code, Java allows you to create well-organized code. Separating error handling and program logic in this way makes it easier to understand and maintain programs in the long run. 
 Java’s exception handling mechanism works in such a way that error reports are propagated up the call stack. This is because whenever an exception occurs, Java’s runtime environment checks the call stack backwards to identify methods that can catch the exception. 
When a program includes several calls between methods, propagation of exceptions up the call stack ensures that exceptions are caught by the right methods.
The exceptions thrown in a Java program are objects of a class. Since the Throwable class overrides the toString() method, you can obtain a description of an exception in the form of a string and display the description using a println() statement. 
catch (ArithmeticException e) {
       System.out.println("Exception occurred: " + e);
}


The above catch statement displays the following output when an arithmetic exception occurs:
Exception: java.lang.ArithmeticException: / by zero
Traditional programming languages use error codes for error reporting. In the case of large programs, debugging errors using their error codes gets complex. The meaningful descriptions provided by Java’s exception handling mechanism are helpful when you need to debug large programs or experiment with complex code.
 Java provides several super classes and sub classes that group exceptions based on their type. While the super classes like IOException provide functionality to handle exceptions of a general type, sub classes like FileNotFoundException provide functionality to handle specific exception types. 
A method can catch and handle a specific exception type by using a sub class object. 
For example, FileNotFoundException is a sub class that only handles a file not found exception. In case a method needs to handle multiple exceptions that are of the same group, you can specify an object of a super class in the method’s catch statement. 
For example, IOException is a super class that handles all IO-related exceptions.
 Happy Learning 🙂

Checked and Unchecked Exceptions in Java
Try with Resources Example in Java
user defined exceptions in Java
Catching Multiple Exceptions in Java 7
Top 10 Exceptions in Java
Thread  join Example in Java
Spring boot exception handling rest service (CRUD) operations
What is Hibernate
Hello World Java Program Example
Advantages of Java Programming
What is Exception in Java
How do we Handle Exception in Java
Exception Chaining and Debugging Example
Java – Types of Polymorphism and Advantages
Top 10 Advantages of Hibernate

Checked and Unchecked Exceptions in Java
Try with Resources Example in Java
user defined exceptions in Java
Catching Multiple Exceptions in Java 7
Top 10 Exceptions in Java
Thread  join Example in Java
Spring boot exception handling rest service (CRUD) operations
What is Hibernate
Hello World Java Program Example
Advantages of Java Programming
What is Exception in Java
How do we Handle Exception in Java
Exception Chaining and Debugging Example
Java – Types of Polymorphism and Advantages
Top 10 Advantages of Hibernate






priya
July 6, 2018 at 11:29 am - Reply 

An Exception in Java is an object which contains information about the error that has occurred. These Exception objects are automatically created when an unexpected situation arises.Thanks for sharing.









Priyanka Vasam
March 20, 2022 at 12:35 pm - Reply 

This page is amazing..it helped me a lot in preparing for my exams










priya
July 6, 2018 at 11:29 am - Reply 

An Exception in Java is an object which contains information about the error that has occurred. These Exception objects are automatically created when an unexpected situation arises.Thanks for sharing.




An Exception in Java is an object which contains information about the error that has occurred. These Exception objects are automatically created when an unexpected situation arises.Thanks for sharing.





Priyanka Vasam
March 20, 2022 at 12:35 pm - Reply 

This page is amazing..it helped me a lot in preparing for my exams




This page is amazing..it helped me a lot in preparing for my exams
 


Δ
 Install Java on Mac OS
 Install AWS CLI on Windows
 Install Minikube on Windows
 Install Docker Toolbox on Windows
 Install SOAPUI on Windows
 Install Gradle on Windows
 Install RabbitMQ on Windows
 Install PuTTY on windows
 Install Mysql on Windows
 Install Hibernate Tools in Eclipse
 Install Elasticsearch on Windows
 Install Maven on Windows
 Install Maven on Ubuntu
 Install Maven on Windows Command
 Add OJDBC jar to Maven Repository
 Install Ant on Windows
 Install RabbitMQ on Windows
 Install Apache Kafka on Ubuntu
 Install Apache Kafka on Windows
 Java8 – Install Windows
 Java8 – foreach
 Java8 – forEach with index
 Java8 – Stream Filter Objects
 Java8 – Comparator Userdefined
 Java8 – GroupingBy
 Java8 – SummingInt
 Java8 – walk ReadFiles
 Java8 – JAVA_HOME on Windows
 Howto – Install Java on Mac OS
 Howto – Convert Iterable to Stream
 Howto – Get common elements from two Lists
 Howto – Convert List to String
 Howto – Concatenate Arrays using Stream
 Howto – Remove duplicates from List
 Howto – Filter null values from Stream
 Howto – Convert List to Map
 Howto – Convert Stream to List
 Howto – Sort a Map
 Howto – Filter a Map
 Howto – Get Current UTC Time
 Howto – Verify an Array contains a specific value
 Howto – Convert ArrayList to Array
 Howto – Read File Line By Line
 Howto – Convert Date to LocalDate
 Howto – Merge Streams
 Howto – Resolve NullPointerException in toMap
 Howto -Get Stream count
 Howto – Get Min and Max values in a Stream
 Howto – Convert InputStream to String