block try-catch is a Java control structure that allows us to handle errors that may be generated by the execution of a block of code, which guarantees that our program does not become unstable and that can end unexpectedly. For the errors that are generated at run time in our program are called exceptions.
The try-catch block is as follows:
Within the block try {... } Attempting to perform a series of actions, while in catch ( exc) {... } are caught any errors that may be generated to execute the series of actions. However, you can add to the try-catch block another code segment:
If segment try {...} no errors occur then the complete execution of this segment is passed to the segment finally {...}, otherwise the segment will run catch {...} block followed finally {...}.
class Throwable ( java.lang package ) is the superclass for all errors and exceptions in Java. Only objects that are instances of this class (or one of its subclasses) are thrown by the Java virtual machine. Exception class (also java.lang package ) and its subclasses are a form of class Throwable , this class shows how applications are responsible for handling errors.
If segment try {...} no errors occur then the complete execution of this segment is passed to the segment finally {...}, otherwise the segment will run catch {...} block followed finally {...}.
class Throwable ( java.lang package ) is the superclass for all errors and exceptions in Java. Only objects that are instances of this class (or one of its subclasses) are thrown by the Java virtual machine. Exception class (also java.lang package ) and its subclasses are a form of class Throwable , this class shows how applications are responsible for handling errors.
Exception class has the following constructors:
Exception ()
Constructs a new exception with a null message.
Exception (String message)
Constructs a new exception with the specified message to display.
Exception (Throwable cause)
Constructs a new exception with a specific cause. Generally class Throwable contains the message of the cause of the error.
Exception (String message, Throwable cause)
Constructs a new exception with detail message specified and the cause.
are many errors that can be generated by running our applications, either by programming errors or by user data entry inersperadas, eg division by zero a number or address to convert a letter into a number. Some, among many other exceptions that are generated are:
ArithmeticException
Occurs when an exceptional arithmetic condition has occurred. Por ejemplo, cuando se trata de dividir un número por cero se lanza una instancia de esta clase.
ClassNotFoundException
Es producida cuando una aplicación trata de hacer referencia a una clase por su nombre y dicha clase no existe.
DataFormatException
Indica que se ha producido un error en el formato de los datos.
InterruptedException
This exception may be generated when a thread is sleeping, waiting or stopped unexpectedly and tries to stop another thread.
IOException
can generate an error in data input or output, for example, file operations and data flows.
NumberFormatException
Launched to indicate that the application attempted to convert a string to a numeric types, but the chain does not have the proper format.
ParseException
Indicates that an error has been reached unexpectedly while trying raalizar an analysis to a sequence of tokens.
PrinterException
Used to indicate that it has generated an error in the printing system.
In Java there are two useful keywords related error handling:
throw
Launches new exception. throws
Indicates that a method can throw an exception. Example: A program that performs the division of two numbers.
Example: A program that determines whether a number is integer or double.