Difference between revisions of "JAVA: try, catch, throw & throws di Java"

From OnnoWiki
Jump to navigation Jump to search
(Created page with "What is an Exception? An exception is an “unwanted or unexpected event”, which occurs during the execution of the program i.e, at run-time, that disrupts the normal flow o...")
 
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
What is an Exception?
+
==Apakah Exception?==
An exception is an “unwanted or unexpected event”, which occurs during the execution of the program i.e, at run-time, that disrupts the normal flow of the program’s instructions. When an exception occurs, execution of the program gets terminated.
 
  
Why does an Exception occur?
+
Exception adalah "peristiwa yang tidak diinginkan atau tidak terduga", yang terjadi selama eksekusi program yaitu, pada saat run-time, yang mengganggu aliran normal instruksi program. Ketika exception terjadi, eksekusi program dihentikan.
An exception can occur due to several reasons like Network connection problem, Bad input provided by user, Opening a non-existing file in your program etc
 
  
Blocks & Keywords used for exception handling
 
  
1.try: The try block contains set of statements where an exception can occur.
+
==Mengapa Exception terjadi?==
  
try
+
Exception dapat terjadi karena beberapa alasan seperti masalah koneksi jaringan, Input buruk yang diberikan oleh pengguna, Membuka file yang tidak ada di program Anda, dll.
{
 
    // statement(s) that might cause exception
 
}
 
2.catch : Catch block is used to handle the uncertain condition of try block. A try block is always followed by a catch block, which handles the exception that occurs in associated try block.
 
  
catch
 
{
 
  // statement(s) that handle an exception
 
  // examples, closing a connection, closing
 
  // file, exiting the process after writing
 
  // details to a log file.
 
}
 
3.throw: Throw keyword is used to transfer control from try block to catch block.
 
  
4.throws: Throws keyword is used for exception handling without try & catch block. It specifies the exceptions that a method can throw to the caller and does not handle itself.
+
==Block & Keyword yang digunakan untuk menangani exception==
  
5.finally: It is executed after catch block. We basically use it to put some common code when there are multiple catch blocks.
+
===1.try:===
  
Example of an Exception generated by system is given below :
+
Blok try berisi serangkaian pernyataan di mana exception dapat terjadi.
  
Exception in thread "main"
 
java.lang.ArithmeticException: divide
 
by zero at ExceptionDemo.main(ExceptionDemo.java:5)
 
ExceptionDemo: The class name
 
main:The method name
 
ExceptionDemo.java:The file name
 
java:5:line number
 
  
// Java program to demonstrate working of try,
+
try
// catch and finally
+
{
 +
    // statement(s) that might cause exception
 +
}
 +
 
 +
===2.catch:===
 +
 
 +
Catch block digunakan untuk menangani kondisi try block yang tidak pasti. Block try selalu diikuti oleh block catch, yang menangani exception yang terjadi di block try terkait.
 +
 
 +
 
 +
catch
 +
{
 +
    // statement(s) that handle an exception
 +
    // examples, closing a connection, closing
 +
    // file, exiting the process after writing
 +
    // details to a log file.
 +
}
 +
 
 +
 
 +
 
 +
==3.throw:==
 +
 
 +
Throw keyword digunakan untuk mentransfer control dari try block ke catch block.
 +
 
 +
 
 +
===4.throws:===
 +
 
 +
Keyword Throws digunakan untuk penanganan exception tanpa block try & catch. Ini menentukan exception yang dapat dilemparkan oleh suatu method ke pemanggil dan tidak menangani dirinya sendiri.
 +
 
 +
 
 +
===5.finally:===
 +
 
 +
Itu dieksekusi setelah catch block. Kita pada dasarnya menggunakannya untuk meletakkan beberapa code umum ketika ada beberapa catch block.
 +
 
 +
Contoh Exception yang dihasilkan oleh sistem diberikan di bawah ini:
 +
 
 +
Exception in thread "main"
 +
java.lang.ArithmeticException: divide
 +
by zero at ExceptionDemo.main(ExceptionDemo.java:5)
 +
ExceptionDemo: The class name
 +
main:The method name
 +
ExceptionDemo.java:The file name
 +
java:5:line number
 +
 +
// Java program to demonstrate working of try,
 +
// catch and finally
 
    
 
    
class Division {
+
class Division {
    public static void main(String[] args)
+
    public static void main(String[] args)
    {
+
    {
        int a = 10, b = 5, c = 5, result;
+
        int a = 10, b = 5, c = 5, result;
        try {
+
        try {
            result = a / (b - c);
+
            result = a / (b - c);
            System.out.println("result" + result);
+
            System.out.println("result" + result);
        }
+
        }
 
+
 
        catch (ArithmeticException e) {
+
        catch (ArithmeticException e) {
            System.out.println("Exception caught:Division by zero");
+
            System.out.println("Exception caught:Division by zero");
        }
+
        }
 
+
 
        finally {
+
        finally {
            System.out.println("I am in final block");
+
            System.out.println("I am in final block");
        }
+
        }
    }
+
    }
}
+
}
 +
 
 
Output:
 
Output:
Exception caught:Division by zero
+
Exception caught:Division by zero
I am in final block
+
I am in final block
  
An example of throws keyword:
+
Contoh throw keyword:
  
 +
// Java program to demonstrate working of throws
 +
class ThrowsExecp {
 +
 
 +
    // This method throws an exception
 +
    // to be handled
 +
    // by caller or caller
 +
    // of caller and so on.
 +
    static void fun() throws IllegalAccessException
 +
    {
 +
        System.out.println("Inside fun(). ");
 +
        throw new IllegalAccessException("demo");
 +
    }
 +
 
 +
    // This is a caller function
 +
    public static void main(String args[])
 +
    {
 +
        try {
 +
            fun();
 +
        }
 +
        catch (IllegalAccessException e) {
 +
            System.out.println("caught in main.");
 +
        }
 +
    }
 +
}
  
// Java program to demonstrate working of throws
 
class ThrowsExecp {
 
 
 
    // This method throws an exception
 
    // to be handled
 
    // by caller or caller
 
    // of caller and so on.
 
    static void fun() throws IllegalAccessException
 
    {
 
        System.out.println("Inside fun(). ");
 
        throw new IllegalAccessException("demo");
 
    }
 
 
 
    // This is a caller function
 
    public static void main(String args[])
 
    {
 
        try {
 
            fun();
 
        }
 
        catch (IllegalAccessException e) {
 
            System.out.println("caught in main.");
 
        }
 
    }
 
}
 
 
Output:
 
Output:
Inside fun().  
+
Inside fun().  
caught in main.
+
caught in main.
  
 
==Referensi==
 
==Referensi==
  
 
* https://www.geeksforgeeks.org/try-catch-throw-and-throws-in-java/
 
* https://www.geeksforgeeks.org/try-catch-throw-and-throws-in-java/

Latest revision as of 16:19, 11 May 2022

Apakah Exception?

Exception adalah "peristiwa yang tidak diinginkan atau tidak terduga", yang terjadi selama eksekusi program yaitu, pada saat run-time, yang mengganggu aliran normal instruksi program. Ketika exception terjadi, eksekusi program dihentikan.


Mengapa Exception terjadi?

Exception dapat terjadi karena beberapa alasan seperti masalah koneksi jaringan, Input buruk yang diberikan oleh pengguna, Membuka file yang tidak ada di program Anda, dll.


Block & Keyword yang digunakan untuk menangani exception

1.try:

Blok try berisi serangkaian pernyataan di mana exception dapat terjadi.


try
{
    // statement(s) that might cause exception
}

2.catch:

Catch block digunakan untuk menangani kondisi try block yang tidak pasti. Block try selalu diikuti oleh block catch, yang menangani exception yang terjadi di block try terkait.


catch
{
   // statement(s) that handle an exception
   // examples, closing a connection, closing
   // file, exiting the process after writing
   // details to a log file.
}


3.throw:

Throw keyword digunakan untuk mentransfer control dari try block ke catch block.


4.throws:

Keyword Throws digunakan untuk penanganan exception tanpa block try & catch. Ini menentukan exception yang dapat dilemparkan oleh suatu method ke pemanggil dan tidak menangani dirinya sendiri.


5.finally:

Itu dieksekusi setelah catch block. Kita pada dasarnya menggunakannya untuk meletakkan beberapa code umum ketika ada beberapa catch block.

Contoh Exception yang dihasilkan oleh sistem diberikan di bawah ini:

Exception in thread "main" 
java.lang.ArithmeticException: divide 
by zero at ExceptionDemo.main(ExceptionDemo.java:5)
ExceptionDemo: The class name
main:The method name 
ExceptionDemo.java:The file name
java:5:line number

// Java program to demonstrate working of try,
// catch and finally
 
class Division {
    public static void main(String[] args)
    {
        int a = 10, b = 5, c = 5, result;
        try {
            result = a / (b - c);
            System.out.println("result" + result);
        }
  
        catch (ArithmeticException e) {
            System.out.println("Exception caught:Division by zero");
        }
  
        finally {
            System.out.println("I am in final block");
        }
    }
}

Output:

Exception caught:Division by zero
I am in final block

Contoh throw keyword:

// Java program to demonstrate working of throws
class ThrowsExecp {
  
    // This method throws an exception
    // to be handled
    // by caller or caller
    // of caller and so on.
    static void fun() throws IllegalAccessException
    {
        System.out.println("Inside fun(). ");
        throw new IllegalAccessException("demo");
    }
  
    // This is a caller function 
    public static void main(String args[])
    {
        try {
            fun();
        }
        catch (IllegalAccessException e) {
            System.out.println("caught in main.");
        }
    }
}

Output:

Inside fun(). 
caught in main.

Referensi