Difference between revisions of "JAVA: Exception"

From OnnoWiki
Jump to navigation Jump to search
Line 1: Line 1:
'''Exception Handling''' in Java is one of the effective means to handle the runtime errors so that the regular flow of the application can be preserved. Java Exception Handling is a mechanism to handle runtime errors such as ClassNotFoundException, IOException, SQLException, RemoteException, etc.
+
'''Exception Handling''' di Java adalah salah satu cara yang efektif untuk menangani kesalahan runtime sehingga aliran reguler aplikasi dapat dipertahankan. Java Exception Handling adalah mekanisme untuk menangani kesalahan runtime seperti ClassNotFoundException, IOException, SQLException, RemoteException, dll.
  
'''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.
+
'''Exception''' adalah peristiwa yang tidak diinginkan atau tidak terduga, yang terjadi selama eksekusi program yaitu pada saat dijalankan, yang mengganggu aliran normal instruksi program. Exception dapat ditangkap dan ditangani oleh program. Ketika exception terjadi dalam suatu method, ini akan menciptakan sebuah object. Object ini disebut exception object. Ini berisi informasi tentang exception seperti nama dan deskripsi exception dan status program saat exception terjadi.
  
Major reasons why an exception Occurs
+
Penyebab utama mengapa sebuah exception terjadi
* Invalid user input
+
* Input user yang salah/invalid
* Device failure
+
* Kegagalan Device
* Loss of network connection
+
* Loss dari sambungan network
* Physical limitations (out of disk memory)
+
* Keterbatasan fisik (out of disk memory)
* Code errors
+
* Error code
* Opening an unavailable file
+
* Membuka file yang tidak ada
  
'''Errors''' represent irrecoverable conditions such as Java virtual machine (JVM) running out of memory, memory leaks, stack overflow errors, library incompatibility, infinite recursion, etc. Errors are usually beyond the control of the programmer and we should not try to handle errors.
+
'''Errors''' mewakili kondisi yang tidak dapat dipulihkan seperti Java virtual machine (JVM) kehabisan memori, memory leaks, stack overflow errors, library incompatibility, infinite recursion, dll. Kesalahan biasanya di luar kendali programmer dan kita sebaiknya tidak mencoba menangani error.
  
Let us discuss the most important part which is the differences between Error and Exception that is as follows:  
+
Mari kita bahas bagian terpenting yang merupakan perbedaan antara Error dan Exception yaitu sebagai berikut:
* '''Error:''' An Error indicates a serious problem that a reasonable application should not try to catch.
+
* '''Error:''' Kesalahan menunjukkan masalah serius yang sebaiknya tidak ditangkap oleh aplikasi yang baik.
* '''Exception:''' Exception indicates conditions that a reasonable application might try to catch.
+
* '''Exception:''' menunjukkan kondisi yang mungkin dapat / dicoba ditangkap oleh aplikasi yang wajar.
  
==Exception Hierarchy==
+
 
All exception and error types are subclasses of class Throwable, which is the base class of the hierarchy. One branch is headed by Exception. This class is used for exceptional conditions that user programs should catch. NullPointerException is an example of such an exception. Another branch, 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.
+
==Hirarki Exception==
 +
 
 +
Semua jenis exception dan error adalah subclass dari class Throwable, yang merupakan base class dari sebuah hirarki. Satu cabang adalah Exception. Class ini digunakan untuk kondisi luar biasa yang harus ditangkap oleh program. NullPointerException adalah contoh pengecualian semacam itu. Cabang lain, Error digunakan oleh Java run-time system (JVM) untuk menunjukkan error yang berkaitan dengan run-time environment itu sendiri (JRE). StackOverflowError adalah contoh error tersebut.
  
 
[[File:Exception-in-java1.png|center|300px|thumb|Exception Hierarchy in Java]]
 
[[File:Exception-in-java1.png|center|300px|thumb|Exception Hierarchy in Java]]
  
==Types of Exceptions==
+
==Tipe Exception==
  
Java defines several types of exceptions that relate to its various class libraries. Java also allows users to define their own exceptions.
+
Java mendefinisikan beberapa jenis exception yang berhubungan dengan berbagai class library. Java juga memungkinkan pengguna untuk menentukan exception mereka sendiri.
  
 
[[File:Type-of-Exception-in-Java.jpg|center|300px|thumb|Types of Exceptions]]
 
[[File:Type-of-Exception-in-Java.jpg|center|300px|thumb|Types of Exceptions]]
  
  
Exceptions can be Categorized in two ways:
+
Exception dapat di kategorikan dalam dua hal:
  
* Built-in Exceptions
+
* Built-in Exception
 
** Checked Exception
 
** Checked Exception
 
** Unchecked Exception  
 
** Unchecked Exception  
* User-Defined Exceptions
+
* User-Defined Exception
  
 
Let us discuss the above-defined listed exception that is as follows:
 
Let us discuss the above-defined listed exception that is as follows:

Revision as of 10:22, 10 May 2022

Exception Handling di Java adalah salah satu cara yang efektif untuk menangani kesalahan runtime sehingga aliran reguler aplikasi dapat dipertahankan. Java Exception Handling adalah mekanisme untuk menangani kesalahan runtime seperti ClassNotFoundException, IOException, SQLException, RemoteException, dll.

Exception adalah peristiwa yang tidak diinginkan atau tidak terduga, yang terjadi selama eksekusi program yaitu pada saat dijalankan, yang mengganggu aliran normal instruksi program. Exception dapat ditangkap dan ditangani oleh program. Ketika exception terjadi dalam suatu method, ini akan menciptakan sebuah object. Object ini disebut exception object. Ini berisi informasi tentang exception seperti nama dan deskripsi exception dan status program saat exception terjadi.

Penyebab utama mengapa sebuah exception terjadi

  • Input user yang salah/invalid
  • Kegagalan Device
  • Loss dari sambungan network
  • Keterbatasan fisik (out of disk memory)
  • Error code
  • Membuka file yang tidak ada

Errors mewakili kondisi yang tidak dapat dipulihkan seperti Java virtual machine (JVM) kehabisan memori, memory leaks, stack overflow errors, library incompatibility, infinite recursion, dll. Kesalahan biasanya di luar kendali programmer dan kita sebaiknya tidak mencoba menangani error.

Mari kita bahas bagian terpenting yang merupakan perbedaan antara Error dan Exception yaitu sebagai berikut:

  • Error: Kesalahan menunjukkan masalah serius yang sebaiknya tidak ditangkap oleh aplikasi yang baik.
  • Exception: menunjukkan kondisi yang mungkin dapat / dicoba ditangkap oleh aplikasi yang wajar.


Hirarki Exception

Semua jenis exception dan error adalah subclass dari class Throwable, yang merupakan base class dari sebuah hirarki. Satu cabang adalah Exception. Class ini digunakan untuk kondisi luar biasa yang harus ditangkap oleh program. NullPointerException adalah contoh pengecualian semacam itu. Cabang lain, Error digunakan oleh Java run-time system (JVM) untuk menunjukkan error yang berkaitan dengan run-time environment itu sendiri (JRE). StackOverflowError adalah contoh error tersebut.

Exception Hierarchy in Java

Tipe Exception

Java mendefinisikan beberapa jenis exception yang berhubungan dengan berbagai class library. Java juga memungkinkan pengguna untuk menentukan exception mereka sendiri.

Types of Exceptions


Exception dapat di kategorikan dalam dua hal:

  • Built-in Exception
    • Checked Exception
    • Unchecked Exception
  • User-Defined Exception

Let us discuss the above-defined listed exception that is as follows:

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: Checked exceptions are called compile-time exceptions because these exceptions are checked at compile-time by the compiler.
  • 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.

Note: For checked vs unchecked exception, see Checked vs Unchecked Exceptions

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’.

The advantages of Exception Handling in Java are as follows:

  • Provision to Complete Program Execution
  • Easy Identification of Program Code and Error-Handling Code
  • Propagation of Errors
  • Meaningful Error Reporting
  • Identifying Error Types

How Does JVM handle an Exception?

Default Exception Handling: Whenever inside a method, if an exception has occurred, the method creates an Object known as an Exception Object and hands it off to the run-time system(JVM). The exception object contains the name and description of the exception and the current state of the program where the exception has occurred. Creating the Exception Object and handling it in the run-time system is called throwing an Exception. There might be a list of the methods that had been called to get to the method where an exception occurred. This ordered list of the methods is called Call Stack. Now the following procedure will happen.

  • The run-time system searches the call stack to find the method that contains a block of code that can handle the occurred exception. The block of the code is called an Exception handler.
  • The run-time system starts searching from the method in which the exception occurred, and proceeds through the call stack in the reverse order in which methods were called.
  • If it finds an appropriate handler then it passes the occurred exception to it. An appropriate handler means the type of the exception object thrown matches the type of the exception object it can handle.
  • If the run-time system searches all the methods on the call stack and couldn’t have found the appropriate handler then the run-time system handover the Exception Object to the default exception handler, which is part of the run-time system. This handler prints the exception information in the following format and terminates the program abnormally.
Exception in thread "xxx" Name of Exception : Description
... ...... ..  // Call Stack

Look at the below diagram to understand the flow of the call stack.

call stack


Illustration:

// Java Program to Demonstrate How Exception Is Thrown
 
// Class
// ThrowsExecp
class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
        // Taking an empty string
        String str = null;
        // Getting length of a string
        System.out.println(str.length());
    }
}

Output:



Let us see an example that illustrates how a run-time system searches for appropriate exception handling code on the call stack.

Contoh:

// Java Program to Demonstrate Exception is Thrown
// How the runTime System Searches Call-Stack
// to Find Appropriate Exception Handler
 
// Class
// ExceptionThrown
class GFG {
 
    // Method 1
    // It throws the Exception(ArithmeticException).
    // Appropriate Exception handler is not found
    // within this method.
    static int divideByZero(int a, int b)
    {
 
        // this statement will cause ArithmeticException
        // (/by zero)
        int i = a / b;
 
        return i;
    }
 
    // The runTime System searches the appropriate
    // Exception handler in method also but couldn't have
    // found. So looking forward on the call stack
    static int computeDivision(int a, int b)
    {
 
        int res = 0;
 
        // Try block to check for exceptions
        try {
 
            res = divideByZero(a, b);
        }
 
        // Catch block to handle NumberFormatException
        // exception Doesn't matches with
        // ArithmeticException
        catch (NumberFormatException ex) {
            // Display message when exception occurs
            System.out.println(
                "NumberFormatException is occurred");
        }
        return res;
    }
 
    // Method 2
    // Found appropriate Exception handler.
    // i.e. matching catch block.
    public static void main(String args[])
    {
 
        int a = 1;
        int b = 0;
 
        // Try block to check for exceptions
        try {
            int i = computeDivision(a, b);
        }
 
        // Catch block to handle ArithmeticException
        // exceptions
        catch (ArithmeticException ex) {
 
            // getMessage() will print description
            // of exception(here / by zero)
            System.out.println(ex.getMessage());
        }
    }
}

Output

/ by zero


Referensi