在面向对象语言里,使用异常(其实也是类)。把出错信息封装到异常类里面,这样当出错的时候,就可以根据捕获的异常对象获取更多的出错信息。
简单的大家都懂,直接上代码:
(1)
public
class
TestSystemIn
{
public
static
void
main
(
String
[]
args
){
try
{
BufferedReader
stdin
=
new
BufferedReader
(
new
InputStreamReader
(
System
.
in
));
System
.
out
.
println
(
"Enter a line:"
);
System
.
out
.
println
(
"111:"
+
stdin
.
readLine
());
}
catch
(
Exception
e
){
}
}
}
(2)
public
class
WriterDemo
{
public
static
void
main
(
String
[]
args
)
throws
Exception {
// 异常抛出, 不处理
// 第1步:使用File类找到一个文件
File
f
=
new
File
(
"c:"
+
File
.
separator
+
"WriterDemo.txt"
);
// 声明File 对象
// 第2步:通过子类实例化父类对象
Writer
out
=
null
;
// 准备好一个输出的对象
out
=
new
FileWriter
(f);
// 第3步:进行写操作
String
str
=
"Hello World!!!"
;
out
.
write
(str);
out
.
flush
();
// 第4步:关闭输出流
out
.
close
();
}
}