Exception Handling

In this section, you will learn about the exception handling with an example and its description.

Exception Handling

In this section, you will learn about the exception handling with an example and its description.

 Exception Handling

Exception Handling


that means exceptional errors. Actually exceptions are used for handling errors in programs that occurs during the program execution. During the program execution if any error occurs and you want to print your own message or the system message about the error then you write the part of the program which generate the error in the
try{} block and catch the errors using catch() block. Exception turns the direction of normal flow of the program control and send to the related catch() block. Error that occurs during the program execution generate a specific object which has the information about the errors occurred in the program.

In the following example code you will see that how the exception handling can be done in java program. This example reads two integer numbers for the variables a and b. If you enter any other character except number ( 0 - 9 ) then the error is caught by NumberFormatException object. After that ex.getMessage() prints the information about the error occurring causes.

Code of the program : 

import java.io.*;

public class exceptionHandle{
  public static void main(String[] args) throws Exception{
  int a, b;
  BufferedReader in = 
new BufferedReader(new InputStreamReader(System.in));
  a = Integer.parseInt(in.readLine());
  b = Integer.parseInt(in.readLine());
  catch (NumberFormatException ex){
  System.out.println(ex.getMessage() + " is not a numeric value.");

Output of this program:

C:\vinod\xml>javac exceptionHandle.java

C:\vinod\xml>java exceptionHandle

For input string: "" is not a numeric value.

An exception is an event that occurs and  interrupts the normal flow of instructions. That is exceptions are objects that store the information about the occurrence of errors. When any kind of error or unusual condition occurs, these exceptions are being thrown. Any exception used to occur earlier always resulted in a program crash. However, some programming languages like java have mechanisms for handling exceptions. This is known as catching exception in Java. The exceptions that occur in the program can be caught using try and catch block.  Remember, the program will crash if the exception would not be caught. There are three types of exceptions in Java. These are -:

1. Checked Exceptions
2. The error
3. Runtime exception

Error and Runtime exceptions are known as unchecked exceptions. This chapter covers how to throw an exception and catch it.
A detailed explanation on the types of exceptions and the advantages of the exceptions.

Basic I/O:
In this section we will learn about basic input and out put operations in Java. Different kinds of sources and destinations are represented by a Stream like disk files, devices, memory arrays etc.  A Stream is a sequence of data. An Input Stream is used by the program to read data from the source. Similarly, a program uses an Output stream to write data to a destination. It also supports different kinds of data including simple bytes, primitive data types, objects etc. We will learn about I/O Steams and how to use them. 

Concurrency is generally used to perform multiple tasks simultaneously. In this section we will learn how to write applications to perform multitasking. There are two types of units of execution process and threads. Thread is the most important unit in concurrent programming. There are many processes and threads which are active in a computer system. However only one threads executes at a time even in systems that only have a single execution core.

Regular expression
The set of strings which are based on common characteristics are shared by each string in the set. Basically, Regular expressions are are a set of strings. 

This regular expression as a Java string, becomes "\\\\". That is 4 backslashes to match a single one. As in literal Java strings the backslash is an escape character. The literal string as a single backslash is denoted by "\\". In this chapter we will learn to create a syntax for regular expressions and how to use them.