Java Programming-Unit IV
Java Programming-Unit IV
I. TYPES OF ERRORS
Compile Time Errors – Errors detected and displayed by java compiler.
Run Time Errors – Program compiles successfully creating .class file but may not run
properly.
Such programs produce wrong results due to wrong logic or may terminate due to
errors.
The common run time errors are:
Dividing an integer by zero.
Array out of bounds.
Trying to store a value into an array of an incompatible class or type.
Passing a parameter that is not in a valid range.
Illegally change the status of thread.
Converting invalid string to number.
Example:
int a = 10, b = 5, c = 5;
int x = a/ (b-c); //Division by zero
Arithmetic Exception
Categories of Exceptions:
Two Types
i. Checked Exceptions: These exceptions are explicitly handled in the code itself with the
help of try-catch blocks. They are extended from java.lang.Exception class.
ii. Unchecked Exceptions: JVM handles such exceptions. They are extended from
java.lang.RuntimeException class.
TRY BLOCK
Causes an Exception
Throws
Exception
CATCH BLOCK
Handles an Exception
2
Try Block:
Keyword 'try' preface a block of code that cause an error and throws exception.
Try block can have one or more statements.
Every 'try' should be followed at least by one 'catch' statement.
If anyone generates exception remaining statement skipped and execution jumps to 'catch'
block.
Catch Block:
The ‘catch’ block catches the exception thrown and handles it appropriately.
The catch statement is passed a single parameter which is reference to exception object, then
exception caught and statements in catch block executed.
Exception not caught default exception handler will terminate execution.
Example:
class exception
{
public static void main(String args[ ])
{
try
{
int a=Integer.parseInt(args[0]);
int b=Integer.parseInt(args[1]);
int c=a/b;
System.out.println(c);
}
catch(Exception d)
{
System.out.println(d);
}
}
}
Output:
D:/java>java exception 10 0
java.lang.ArithmeticException /by zero
3
Multiple Catch Statements:
General form:
try
{
Statement;
}
Catch (Exception-Type-1 e)
{
Statement;
}
catch(Exception-Type-2 e)
{
Statement;
}
.
.
catch(Exception-Type-N e)
{
Statement;
}
There can be more than one catch statements in java. When an exception in try block
generated, the first statement whose parameter matches with the exception object will be
executed, and remaining statements skipped.
4
The finally statement is used to handle an exception that is not caught by any previous catch
statements.
It is added immediately after try or after last catch block.
Input Stream Classes - Super class Input Stream; cannot create instances, it is an abstract
class.
Object
INPUT STREAM
5
Byte Array Input Stream
String Buffer Input Stream
Filter Input Stream
Buffered Input Stream
Pushback Input Stream
Data Input Stream
Data Input(Interface)
Methods
readShort( )
readInt( )
readLong( )
readFloat( )
readDouble( )
readLline( )
readChar( )
readBoolean( )
Char Array Char Array Byte Array Input Byte Array Output
Memory
Reader Writer Stream Stream
File File Reader File Writer File Input Stream File Output Stream
Pipe Piped Reader Piped Writer Piped Input Stream Piped Output Stream
8
public static void main(String args[])throwsIOException
{
try
{
DataInputStream din=new DataInputStream(System.in);
String sfile,dfile;
int n=0;
System.out.println("FileCopy");
System.out.println("Enter the Source File");
sfile=din.readLine();
System.out.println(sfile);
System.out.println("Enter the Destination File");
dfile=din.readLine();
System.out.println(dfile);
FileReader fr=new FileReader(sfile);
FileWriter fw=new FileWriter(dfile);
while((n= fr.read())!=-1)
{
fw.write(n);
}
System.out.println("File Copied");
fr.close();
fw.close();
}
catch(Exception e)
{
System.out.println("File not Found");
}
}
}
---------