try{
//这里面要有异常抛出的可能
}catch(Exception e){
//处理抛出的异常。
}
给你一个例子,意思是输入两个数相加,你看看就明白了。
import java.io.*;public class CaretakerTest {
public static void main(String[] args) {
System.out.println("----- Please edit text -----");
try {
System.out.print("Enter A: ");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String strA = in.readLine();
int a = Integer.parseInt(strA); System.out.print("Enter B: ");
in = new BufferedReader(new InputStreamReader(System.in)); String strB = in.readLine();
int b = Integer.parseInt(strB); System.out.println(a + b);
}
catch (IOException e) {
System.out.println("I/O Error !");
System.exit(1);
}catch(NumberFormatException e){
System.out.println("Prease enter number!");
}
}
}
//这里面要有异常抛出的可能
}catch(Exception e){
//处理抛出的异常。
}
给你一个例子,意思是输入两个数相加,你看看就明白了。
import java.io.*;public class CaretakerTest {
public static void main(String[] args) {
System.out.println("----- Please edit text -----");
try {
System.out.print("Enter A: ");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String strA = in.readLine();
int a = Integer.parseInt(strA); System.out.print("Enter B: ");
in = new BufferedReader(new InputStreamReader(System.in)); String strB = in.readLine();
int b = Integer.parseInt(strB); System.out.println(a + b);
}
catch (IOException e) {
System.out.println("I/O Error !");
System.exit(1);
}catch(NumberFormatException e){
System.out.println("Prease enter number!");
}
}
}
你定义了通用异常,方便了自己,可苦了JAVA编译器,还得帮你分辨异常的类型,
那java还不得更慢了
public class NotPluralExceptionextends Exception{
public NotPluralException(String msg){
super(msg);
}
}
然后在你程序中可以用自己定义的异常
public class Plural{
int value ;
public Plural(int i,int t) throws NotPluralException{
value = i * i;
if(value/1 == value){
}else{
throws new NotPluralException("不是复数");
}
}
}
所以在捕捉的时候就可以写
try{
...
}catch(NotPluralException e){
e.printStackTrace();
}
http://www-900.ibm.com/developerWorks/cn/cnedu.nsf/java-onlinecourse-bytitle?OpenView&Count=500
是因为程序经常回出现这样或那样的问题。为了解决这个问题,java采用这种结构,因为程序不能在如何地方都停下来。而且要让人知道是什么地方出了问题所以就用这中异常类来描述问题的情况、
哈哈,,
年轻的牛和老牛拉的车都是牛车!!!
用,
try
{
}
catch(Exception e)
{}
基本上可以通用,,
但是正如上面那位兄弟所说,如果不确定是何种异常就会慢,,,,,,
但对于初学者来说,可以经过一段长时间学习来慢慢去掌声握,先要会实现,
然后才是效率的提高,
interface InterfaceName{
声明实例域\\所有声明的变量都自动加上final 和static修饰
声明方法\\注意方法只是声明,没有方法体
}
java通过接口实现多重继承
比如class MyFrame extends Jframe implements ActionListener{
....
void ActionPerformed(ActionEvent e){
}//这个方法一定得写,否则不能编译。也就是说凡是接口中声明的方法在实现的类中都要重写
}这里只谈到一小点,不知是否有用
定义对象和别处没什么两样嘛
只是里面定义的是局部的通常的异常就有十几种
它们都来源于Throwable类,
该类派生出两个类Error和Exception
我们一般用的都派生于Exception类
如IOException等