RuntimeException menurut Java Documentation
"
RuntimeException
is the superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine. A method is not required to declare in its throws
clause any subclasses of RuntimeException
that might be thrown during the execution of the method but not caught"diterjemahkan :
RuntimeException adalah superclass dari exception yang dapat timbul selama pengoperasian normal pada Java Virtual Machine, Sebuah metode tidak diperlukan memuatnya pada kalimat throws tiap subclass dari RuntimeException yang mungkin dikeluarkan selama eksekusi dari metode tetapi tidak ditangkap.
Sedangkan NullPointerException menurut Java Documentation
"Thrown when an application attempts to use
null
in a case where an object is required. These include: - Calling the instance method of a
null
object. - Accessing or modifying the field of a
null
object. - Taking the length of
null
as if it were an array. - Accessing or modifying the slots of
null
as if it were an array. - Throwing
null
as if it were aThrowable
value.
Applications should throw instances of this class to indicate other illegal uses of the null
object."
diterjemahkan :
Dikeluarkan ketika sebuah aplikasi mencoba menggunakan null dalam sebuah kasus ketika sebuah object diperlukan. Ini termasuk :
- Memanggil sebuah instance method dari sebuah null object.
- Mengubah atau mengakses variabel dari null object.
- Mengambil panjang dari sebuah null ketika hal tersebut sebuah array.
- Mengubah atau mengakses sebuah null ketika hal tersebut sebuah array.
- Mengeluarkan null ketika hal tersebut adalah sebuah Throwable value.
Di dokumentasi RuntimeException mengatakan tidak perlu ditangkap, sedangkan di NullPointerException mengatakan exception tersebut perlu ditangkap. Hal tersebut sesuai dengan dokumentasi Java "Unchecked Exception - The Controversy" dimana mengatakan
"Runtime exceptions can occur anywhere in a program, and in a typical one they can be very numerous. Having to add runtime exceptions in every method declaration would reduce a program's clarity. Thus, the compiler does not require that you catch or specify runtime exceptions (although you can). "
artinya :
"Runtime exception dapat terjadi dimana saja di sebuah program, dan pada tipe tertentu mereka dapat banyak sekali. Menambah runtime exception di tiap deklarasi sebuah metode akan mengurangi kejernihan program. Jadi, compiler tidak mengharuskan anda menangkap dan menentukan runtime exception (meskipuan anda bisa)."
NullPointerException saya kemarin terjadi karena code
Map
maps.put("tanggal", user.getTanggal());
user.getTanggal bernilai null maka timbullah NullPointerException. Jadi menurut saya mempersiapkan exception handler pada setiap kode yang memungkinkan NullPointerException lebih baik daripada kebelakangnya pada saat menangani sebuah error / bug yang ternyata bisa disiapkan exception handler-nya di awal kita harus mengikuti software development process (Contoh : waterfall model) :
1. Requirement
2. Software Design
3. Integration
4. Testing
5. Deployment
6. Maintenance
Tidak ada komentar:
Posting Komentar