0% found this document useful (0 votes)
21 views3 pages

Exception Handling

The document discusses Java library classes and packages, input/output streams, wrapper classes, errors, and exception handling in Java. Library classes are predefined Java classes grouped into packages for standard functionality. Common packages include java.lang, java.io, java.util. Streams are used for input/output and can be byte-oriented or character-oriented. Wrapper classes allow primitive types to be used as objects. Errors can occur at compile-time, run-time, or be logical errors. Exception handling uses try/catch blocks and throws keywords to handle errors gracefully.

Uploaded by

zemo kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
21 views3 pages

Exception Handling

The document discusses Java library classes and packages, input/output streams, wrapper classes, errors, and exception handling in Java. Library classes are predefined Java classes grouped into packages for standard functionality. Common packages include java.lang, java.io, java.util. Streams are used for input/output and can be byte-oriented or character-oriented. Wrapper classes allow primitive types to be used as objects. Errors can occur at compile-time, run-time, or be logical errors. Exception handling uses try/catch blocks and throws keywords to handle errors gracefully.

Uploaded by

zemo kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 3

Library Classes

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.

Stream : stream is flow of data from source to destination.


Input stream : in input stream data is read from the source. Source of data can be a file,
keyboard or computer memory.

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 : Exception in general refers to some contradictory or unusual situation


which can be encountered while executing a program. the way to handle exception is called
exception handling.

Advantages of Exception Handling :


(i) Exception handling separates error handling code from normal code.
(ii) It clarifies the code and enhanced readability.
(iii) It makes for clear, robust, fault tolerant programs.

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.

1 - try / catch / finally blocks :


Try block contains the code which might lead to run time errors.
Catch block contains the code to deal with an exception that might arouse due to execution of a
try block.
Finally block contains the code that gets executed when no other block is able to handle the
exception.
class Error
{
public void testfunction (int x, int y)
{
int z;
try
{
z = x / y;
System.out.println( z );
}
catch (exception exp)
{
System.out.println(exp.getMessage( ) );
}
finally
{
System.out.println(“Un–reported exception has occurred…”);
}
}

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
{

You might also like