Exception Handling
Exception Handling
Library Classes of java : pre defined classes of java which get included in an application
program itself are called the Library classes. They are also known as standard java classes or
built-in java classes. These library classes are contained in packages.
Java Packages : Packages are collection of similar nature classes. Java contains an extensive
library of pre-written classes grouped together into packages .
A package can be created by using the keyword ‘package’ and the keyword used to include a
package in our program is ‘import’.
java.lang.* It is a default package containing String, Math, Integer etc. Classes.
java.io.* It is the basic Input Output package of Java
java.util.* The java utility package
Input and Output in java: java.io package provides a set of InputStreams and
OutputStreams used to read and write data file or input and output devices.
Output stream : the data is written to destination using output stream. The destination can be
a file or monitor.
In java two types of stream is present :
1- Byte Stream.
2- Character Stream.
Byte Oriented IO Stream : Byte oriented IO stream reads or write bytes of data where there is
no notation of datatypes. In Java it is performed through DataInput Stream and
DataOutputStream classes.
Character Oriented IO Stream : Character oriented IO Stream performs IO operations on
characters. it is performed through Readers and Writers classes.
Scanner Class : Scanner is a library class and it is used to take input from the user.
Scanner class is defined inside the java.util package.
Their are different functions of Scanner class for taking input of different data types.
Syntax:
import <package name>;
<Scanner> <object name> = new <Scanner(System.in);>
import java.util.*;
Scanner sc = new Scanner(System.in);
int a = sc.nextInt() //to input integer from keyboard
float b = sc.nextFloat() //to input float value from keyboard
double c = sc.nextDouble() //to input double value from keyboard
char d = sc.next().charAt(0); //to input character from keyboard
String s = sc.nextLine() //to input string from keyboard
BufferedReader class: : it is a library class and it is used to take input from the user.
BufferedReader class is defined inside the java.io package.
Syntax :
import java.io.*;
BufferedReader br= = new BufferedReader(new InputStreamReader(System.in));
int a = Integer.parseInt(br.readLine()) //to input integer from keyboard
float b = Float.parseFloat(br.readLine()) //to input float value from keyboard
double c = Double.parseDouble(br.readLine()) //to input double value from keyboard
char d = (char)br.read(); //to input character from keyboard
String s = br.readLine() //to input string from keyboard
Wrapper Classes :Wrapper classes are a part of Java’s standard default library java.lang .
Wrapper classes wrap the primitive datatypes into respective objects.There is a wrapper class for
every primitive date type in Java. For instance the wrapper class for int is Integer, for float is
Float, for char is Character, and so on .
A wrapper class is needed to store primitive values in objects as well as in conversion from
string to primitive type.
Types of Errors :
1. Compile time errors / Syntax Errors – The errors that occurs as a result of violating
the programming rules or syntax are called Compile time errors. They get detected at the
time of program compilation.
e.g. int class = 10; // class is a keyword hence syntactically incorrect.
2. Run time errors – The errors that occurs during program execution is called run time
errors. These unexpected errors may occur due to array index going out of bound, end of
file reached, type mismatch errors at the time of data input etc…
3. Logical Errors – These errors are those which occur due to misinterpretation of the
problem / program logic. like if the problem says to add two numbers and the
programmer commits a multiplication on the numbers, such programs might be
syntactically totally correct but the logic was misinterpreted.
Exception handling is a transparent and an efficient way of handling run time errors.
In java two ways of handling exception :
1. Try – catch block.
2. Throws keyword.
2-Throws : it is a keyword which is used to inform that an error has occurred, it is specified in
the line of function definition (prototype).
The program below gives an idea as to how an Exception is handled in Java.
class Error
{
public static void main () throws IOException
{