package com.hao.test;
public class test2 {
static String cc="this is a test2.";
public static String getinfo() throws Exception{
if (true) {
throw new Exception();
}
return cc;
}
}
主程序:
public class Test extends Activity implements OnClickListener {
private TextView textView1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
try {
test2.getinfo();//在此调用,出现异常,所以执行catch中的程序。
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
textView1.setText("因为抛出异常,所以这里用try捕获,catch 处理此异常 ");
}
handler();
// settext();
}
本文探讨了在Java编程中如何使用try-catch块来捕获并处理异常,通过一个具体的例子展示了异常处理机制的实际应用。
12万+

被折叠的 条评论
为什么被折叠?



