Sunday, May 31, 2015

Exception Handling


Exception Handling :

  • Input & Output Exception 
  • SQL Exception,
  • Runtime Exception,

Runtime Exception:

  1. Arithmatic Exception
  2. Nullpointer Exception,
  3. Number Format Exception,

The Exception Three  types,,

  1. Checked...
  2. Unchecked....
  3. Error...

 Checked Exception.....

The checked exception is only compile time error checked the error,,

IOException Error..
SQLException Error.

Unchecked Exception..

The Unchecked exception is only Runtime error checked the error...

ArithmaticException..
NullPointerException..
ArrayIndexOutof BoundsException..

Error;

Error is irrecoverable..

OutOfMemoryError,
VirtualMachineError,
AssertionError

Input & Output Stream

Output Stream

     The java application used to the output stream write the data to destination.


5 types below;

  1. FileOutputStream,
  2. ByteArrayOutputStream,
  3. FilterOutputStream,
  4. PipedOutputStream,
  5. ObjectOutputStream,


Filter OutputStream 

3 types below:

  1. DataOutputStream
  2. BufferedOutputStream
  3. printStream

Input Stream

   The java application used to the input stream in read data from source...


6 types below:

  1. FileInputStream
  2. ByteArrayInputStream,
  3. FilterInpuStream,
  4. SequenceInputStream
  5. PipedInputStream
  6. ObjectInputStream,

FilterInputStream

3 types :

  1. DataInputStream,
  2. BufferedInputStream,
  3. PushbackInputStream

File Input Stream


Program:


public class Demo {

public static void main(String[] args) {
try{  
   FileInputStream fin=new FileInputStream("D:\\MANIKATTI.K.doc");  
   int i=0;  
   while((i=fin.read())!=-1){  
    System.out.println((char)i);  
   }  
   fin.close();  
 }catch(Exception e){System.out.println(e);}  
}

}


File Output Stream

Program :



public class Demo {

public static void main(String[] args) {
try{
FileOutputStream fin=new FileOutputStream("D:\\MANIKATTI.K.doc");
String a="Welcome to Friends";
byte[] b=a.getBytes();
fin.write(b);
fin.close();
System.out.println("Success");
}catch(Exception e){System.out.println(e);}
}

}



Byte Array Output Stream


public class Demo1 {

public static void main(String[] args) throws IOException {
try {
FileOutputStream f=new FileOutputStream("D:\\MANI.doc");
FileOutputStream f1=new FileOutputStream("D:\\MANIKATTI.K.doc");
String a="welcome";
byte[] s=a.getBytes();
f.write(s);
ByteArrayOutputStream by=new ByteArrayOutputStream();
by.write(s);
by.writeTo(f);
by.writeTo(f1);
by.flush();
by.close();
System.out.println("true");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

}


Sequence InputStream :


public class Demo1 {

public static void main(String[] args) throws Exception {
try {
FileInputStream f=new FileInputStream("D:\\MANI.doc");
FileInputStream f1=new FileInputStream("D:\\MANIKATTI.K.doc");
SequenceInputStream s=new SequenceInputStream(f, f1);
int i;
while((i=s.read())!=-1){
System.out.println((char)i);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

}


public class Demo2 {

public static void main(String[] args) throws Exception {

try {
FileInputStream f=new FileInputStream("D:\\MANI.doc");
FileInputStream f1=new FileInputStream("D:\\MANIKATTI.K.doc");
FileOutputStream fout=new FileOutputStream("D:\\MANIK.doc");

SequenceInputStream sis=new SequenceInputStream(f,f1);
int i;
while((i=sis.read())!=-1){
fout.write(i);
}
f.close();
f1.close();
fout.close();
sis.close();

} catch (FileNotFoundException e) {
e.printStackTrace();
}

}

}