Sei sulla pagina 1di 3

ClassNotFoundException. ...

FileNotFoundException. ...
IOException. ...
InterruptedException. ...
NoSuchFieldException. ...
NoSuchMethodException
reference:www.geeksforgeeks.com

class not found exception example


public BadClassFiles() throws ClassNotFoundException {
classes = new Class<?>[] {
loader.defineClass("EmptyName", EmptyName_bytes),
loader.defineClass("BadModifiers", BadModifiers_bytes),
loader.defineClass("BadNameIndex", BadNameIndex_bytes),
loader.defineClass("NameIndexOutOfBounds", NameIndexOutOfBounds_bytes),
loader.defineClass("ExtraParams", ExtraParams_bytes),
// Name with .
loader.defineClass("BadName1", BadName1_bytes),
// Name with [
loader.defineClass("BadName2", BadName2_bytes),
// Name with ;
loader.defineClass("BadName3", BadName3_bytes),
// Name with /
loader.defineClass("BadName4", BadName4_bytes)
};
}

IO exceptions example
public void inflate(int menuRes,Menu menu){
XmlResourceParser parser=null;
try {
parser=mContext.getResources().getLayout(menuRes);
AttributeSet attrs=Xml.asAttributeSet(parser);
parseMenu(parser,attrs,menu);
}
catch ( XmlPullParserException e) {
throw new InflateException("Error inflating menu XML",e);
}
catch ( IOException e) {
throw new InflateException("Error inflating menu XML",e);
}
finally {
if (parser != null) parser.close();
}
}
reference: www.javased.com

file not found exception example


import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class FileNotFoundExceptionExample_v2 {

private static final String filename = "input.txt";

public static void main(String[] args) {


BufferedWriter wr = null;
try {
// Open the file for writing, without removing its current content.
wr = new BufferedWriter(new FileWriter(new File(filename), true));

// Write a sample string to the end of the file.


wr.write("A sample string to be written at the end of the file!\n");
}
catch(IOException ex) {
System.err.println("An IOException was caught!");
ex.printStackTrace();
}
finally {
// Close the file.
try {
wr.close();
}
catch (IOException ex) {
System.err.println("An IOException was caught!");
ex.printStackTrace();
}
}
}
}
reference:www.javacodegeeks.com
public Object clone() {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(this);
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);
return ois.readObject();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
return null;
}
}
Reference : www.programcreek.com

Interrupted exceptions
static public void spawnProcess(final List<String> cmd) {
if (cmd == null)
return;
Thread t = new Thread() {
public void run() {
Process proc = null;
proc = startProcess(cmd, true);
if (proc != null) {
consumeProcessOutput(proc);
try {
proc.waitFor();
}
catch (InterruptedException e) {
// ignore (we terminate anyway)
}
destroyProcess(proc);
}
Thread.currentThread().interrupt();
}
};
t.setDaemon(true);
t.start();
}
reference: www.programcreek.com
no such method exception
public static void main(String argv[]) throws NoSuchFieldException,
NoSuchMethodException {
if (argv.length == 1 && argv[0].equals("buildagent")) {
buildAgent();
return;
}

if (inst == null) {
throw new RuntimeException("Instrumentation object was null");
}

RedefineAnnotations test = new RedefineAnnotations();


test.testTransformAndVerify();
}
reference:www.programcreek.com

2.A java program to read the content of a file in a console application


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class ReadFileBufferedReader {

private static String filepath = "C:\Users\NANAKAY\Desktop\data.txt";

public static void main(String[] args) {

BufferedReader br;
String curline;
try {
br = new BufferedReader(new FileReader(filepath));

while ((curline = br.readLine()) != null) {


System.out.println(curline);
}

br.close();
} catch (IOException e) {
e.printStackTrace();
}
}

Potrebbero piacerti anche