E2EHIRING Logo
Jobs
Jobs
courses
Courses
mentorship
Mentorship
more
Moredropdown
E2EHIRING Logo
more
Jobs
Jobs
courses
Courses
mentorship
Mentorship
HomeSepratorIconBlogsSepratorIconExceptions in JavaSepratorIcon

Exceptions in Java

Han Soloshahbaz peerzade
calendar19 Jul 2022
poster

Exception Handling 

 Exception Handling is a mechanism to handle runtime errors such as ClassNotFoundException, IOException, SQLException, RemoteException,ArthmeticException,NullPointerException etc.

Exception is an unwanted or unexpected event, which occurs during the execution of a program, i.e. at run time, that disrupts the normal flow of the program’s instructions. Exceptions can be caught and handled by the program. When an exception occurs within a method, it creates an object. This object is called the exception object. It contains information about the exception, such as the name and description of the exception and the state of the program when the exception occurred. 

Major reasons why an exception Occurs

  • Invalid user input
  • Device failure
  • Loss of network connection
  • Physical limitations (out of disk memory)
  • Code errors
  • Opening an unavailable file

Differences between Error and Exception that is as follows: 

  • Error:

 An Error indicates a serious problem that a reasonable application should not try to catch.

  • Exception:

 Exception indicates conditions that a reasonable application might try to catch

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. User-Defined Exception
    • Checked Exception
  2. Built-in Exceptions
    • Unchecked Exception

User-Defined Exceptions:

Sometimes, the built-in exceptions in Java are not able to describe a certain situation. In such cases, users can also create exceptions, which are called ‘user-defined Exceptions’.

  • Checked Exceptions: Checked exceptions are called compile-time exceptions because these exceptions are checked at compile-time by the compiler.

Built-in Exceptions:

Built-in exceptions that are available in Java libraries. These exceptions are suitable to explain certain error situations.

Unchecked Exceptions: 

The unchecked exceptions are just opposite to the checked exceptions. The compiler will not check these exceptions at compile time. 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.

Advantage of Exception Handling

The core advantage of exception handling is to maintain the normal flow of the application. An exception normally disrupts the normal flow of the application; that is why we need to handle exceptions. Let's consider a scenario:

 Example 
   statement 1;
   statement 2;
   statement 3;
   statement 4;
   statement 5;//exception occurs 
   statement 6;
   statement 7;
   statement 8;
   statement 9;
   statement 10;

 

Suppose there are 10 statements in a Java program and an exception occurs at statement 5; the rest of the code will not be executed, i.e., statements 6 to 10 will not be executed. However, when we perform exception handling, the rest of the statements will be executed. 

public class JavaExceptionExample{ 

        

              public static void main(String args[]){  

               try{  

                  //code that may raise exception  

                  int data=100/0;  

               }catch(ArithmeticException e){

                   System.out.println(e);

                   }  

               }

               //rest code of the program  

               int a=10+5;

               System.out.println("rest of the code...");  

               System.out.println(a);

              }  

            Out put//       

               java.lang.ArithmeticException: / by zero

                rest of the code...

                  a=15



   

In the above example, 100/0 raises an ArithmeticException which is handled by a try-catch block. 

throw 

                  Java throw keyword is used to throw an exception explicitly in the code, inside the function, or the block of code.

  • type of exception using throw keyword, we can propagate unchecked exception the checked exception cannot be propagated 

using throw only  

  • throw  is used  within  the method 
  • We are allowed to throw only one exception at a time we cannot throw multiple exceptions.

throws 

            Java throw keyword is used in the method signature to declare an exception that might be thrown by the function while the execution of the code 

  • Using the throws keyword, we can declare both checked and unchecked exceptions. However, the throws keyword can be used to propagate checked exceptions only 
  • throws is used with the method signature.
  • We can declare multiple exceptions using throws keyword that can be thrown by the method. For example, main() throws IOException, SQLException 




Recent Posts

Rapid Changes in the Coding World: Need for High Skilled Programmers e2eHiring Bridges this Gap with the Mentorship Program April 2023

Rapid Changes in the Coding World: Need for High Skilled Programmers e2eHiring Bridges this Gap with the Mentorship Program April 2023

How to publish your Android app on Google Play Store

How to publish your Android app on Google Play Store

Creating Dynamic User Interfaces with Android Motion Layout

Creating Dynamic User Interfaces with Android Motion Layout

Bean Life Cycle

Bean Life Cycle

Pom.XML

Pom.XML

copycopycopycopy

Han Solo

Recent Posts

Rapid Changes in the Coding World: Need for High Skilled Programmers e2eHiring Bridges this Gap with the Mentorship Program April 2023

Rapid Changes in the Coding World: Need for High Skilled Programmers e2eHiring Bridges this Gap with the Mentorship Program April 2023

How to publish your Android app on Google Play Store

How to publish your Android app on Google Play Store

Creating Dynamic User Interfaces with Android Motion Layout

Creating Dynamic User Interfaces with Android Motion Layout

Bean Life Cycle

Bean Life Cycle

Pom.XML

Pom.XML