Exception on Object-Oriented Programming with Java and Its Implementation

Thebestindonesia.com – When running a program, we often encounter errors. The error can come from hardware, software, or algorithm, usually in the form of a logic error. Each of these errors has different solutions and different handling techniques as well.

Hardware and Software Errors

  1. Hardware
    When an application tries to open a file that doesn’t exist, for example, sending characters to a printer that is off or when communicating with an unresponsive serial port.
  2. Software
    When the code tries to access an element that is outside the array’s bounds or tries to store a value that exceeds the capacity of the data format, software-related errors must be detected.
  3. Hardware and Software
    An application might check the divisor operand to ensure that a divisor with zero was not attempted. However, if a division by zero occurs, the hardware in most computer systems generates an error response.

Algorithm Error
It is related to errors or intrinsic limitations of the real-world modeling that is done by the computer. A scientific discipline called numerical analysis emerged to overcome rounding and truncating errors in various algorithms. This type of error is the one most often overlooked by programmers.

  1. Exceptions
    The exception is an abnormal/unnatural condition when executing a command. Below is a list of important exceptions in Java.
  2. ArithmeticExcepJava.
    The exception is thrown when an excellent condition has occurred in an arithmetic operation.
  3. ArrayIndexOutBoundsException.
    An exception indicates that the array has been accessed with an illegal index. The index is negative or greater than or equal to the size of the collection.
  4. ClassNotFoundException.
    This exception appears when we try Exception, a class whose definition is not found.
  5. FileNotFoundException.
    This exception is raised when the Exception is accessed or opened.
  6. IOException. Exceptions are thrown when an input-output operation fails or is interrupted.

In java, five keywords are used to handJavahis exception: try, catch, finally, throw, and throws. Exceptioneption classes are in java.lang package).

Try Catch

Used to handle exceptions tJavaoccur in the program, you put the code that you want to inspect for possible exceptions in the try block, followed by a catch block which determines what type of exception you want to handle.
Writing structure:

try{
…//program blExceptionch(typeException e){
…//program block
}

Throw

The throw mechanism allows the program to send/throw exceptions, and then the program responds (reacts) to these exceptions. Exception objects are all objects that inherit from the class Throwable. One example of an exception object is ArrayIndexOutOfBounds.

Read also : Paradigm of Object Oriented Programming

Writing structure:

Throw OBjectException;
or
classNameClass throws Exception{
….
}
or function/procedure
int f1() Exception{
….
}

Finally

The try-catch block is sometimes confusing because we cannot determine exactly which path to execute. Moreover, the use of throw causes the code after the throw to not be executed, or an error occurs in the catch block, causing the program to stop. To solve this problem, Java introduced the final keyword, where all the code in the last block wave is executed regardless of what happens in the try-catch block.
Writing structure:

try{
…//program block
} catch(typeException1 e){
…//program block for TypeException1
} catch(typeException2 e){
…//program block for TypeException2
} finally {
…//program block
}

I/O (Input and Output)
Stream is a process flow of data information represented abstractly for writing or producing (output) and reading or getting some information (input). All streams have the same properties even though the physical equipment associated with them is different, for example, the keyboard, network, etc.

Packages that handle reading and writing info for Java I/O programming have two types: Byte Stream and Character Stream.

Read also : Graphical User Interface (GUI) on Object-Oriented Programming with Java 

Byte Streams

Byte Streams are used for I/O operations that use binary data (bytes). In java byte stream, there are two superclasses, InputStream and OutputStream, which are abstract classes.

Character Streams

It handles I/O operations that use characters and is an object that can read and write byte streams. So, the character stream is a byte forwarded by the Reader and Writer classes which are Abstract classes.

Tinggalkan Balasan

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *