Java by API/java.lang/Throwable — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 14:40, 31 мая 2010
Throwable as a type
/*
* Output:
*
* a = 0
got t: java.lang.ArithmeticException: / by zero
*
*/
public class MainClass {
public static void main(String args[]) {
try {
int a = args.length;
System.out.println("a = " + a);
int b = 42 / a;
int c[] = { 1 };
c[42] = 99;
} catch(Throwable t) {
System.out.println("got t: " + t);
}
}
}
Throwable: printStackTrace()
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
public class MainClass {
public static void main(String[] args) {
try {
File tempFile = File.createTempFile("myfile", ".tmp");
FileOutputStream fout = new FileOutputStream(tempFile);
PrintStream out = new PrintStream(fout);
out.println("some text");
} catch (IOException ex) {
System.out.println("There was a problem creating/writing to the temp file");
ex.printStackTrace();
}
}
}