异常处理的问题 在程序中,对异常的识别和处理对我还是个难点,请问,具体在写程序的过程中,我们怎么样识别一段程序该捕获些什么异常呢,还有,系统异常和应用异常有什么区别,能不能简单举个例子。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Integerpublic Integer(String s) throws NumberFormatException NumberFormatException当应用程序试图将字符串转换成一种数值类型,但该字符串不能转换为适当格式时,抛出该异常.throws代表调用方法的调用方进行处理,参考.. 用个好点的IDE,就会提示你该捕获什么异常其次,系统异常是不用你去写代码捕获的,自己写的就需要捕获,可以写try,catch,不写的话,直接在方法或类名的后面申明会抛出什么异常 异常应该分为runtimeExpetion 和普通异常,runtimeExpetion 就是在运行是发生的异常~~比如null指针的异常等,普通异常就如2楼的,是必要捕获的~要作出相应的处理, 用eclipse和jb编译会给提示的啊,慢慢积累吧 /*java中编译错误由编译程序javac产生,运行时的错误通常成为(exception),异常是java中的类.java的异常全部在java.lang.Exception中,RuntimeException是其中一大类,而ArithmeticException属于此类。整数除零会产生ArithmeticException,浮点数除零,就不会产生异常,此是程序设计的特点使用java的异常处理机制,只要简单地把可能出现错误的段落包括在try{}中即可,然后,紧接着try之后,用catch(Exception e)输出错误的情况.try内的语句如果出现异常,都会产生或者叫throws抛出一个异常对象e给catch。如果try内的某条语句产生异常,那么程序不会执行其后的try内的语句,而是转向执行catch内的语句,然后接着执行catch后的语句,程序仍然能够正常结束提醒: try的意思就是试试,试试其中的语句是否会发生异常,catch的意思是抓住、捕获, 如果有异常发生,就抓住它,并在catch中进行处理。Throwable类的三种方法:getMessage(),toString(),printStackTrace().*/public class TestException{ private int i; static int k; public static void main(String[] args){ try{ /////////////////////////////////////////////////////////////////// //测试浮点数除零,就不会产生异常 double z = 3; //可以不赋成3.0 但输出成3.0 System.out.println("double z = " + z); double s = z/0; System.out.println("double s = " + s); //不产生异常,输出Infinity(无穷大) ///////////////////////////////////////////////////////////////// //测试整数除零会产生ArithmeticException int y; TestException te = new TestException(); te.i = 3; //仍然在同一个类中,可以访问private成员变量i y = 3/te.i; //产生异常 ArithmeticException: by/zreo System.out.println("))))))))))))))))))))))))))))))))))"); System.out.println("int y = " + y); te = null; System.out.println(te.i); }catch(ArithmeticException e) { System.out.println("ArithmeticException found!"); System.out.println(e.getMessage()); System.out.println("-------------Exception ArithmeticException end location-------------"); return; }catch(NullPointerException e){ System.out.println("NullPointerException found!"); System.out.println(e.getMessage()); System.out.println(); System.out.println(e.toString()); e.printStackTrace(); System.out.println("-------------Exception NullPointerException end location-------------"); }finally{ System.out.println(); System.out.println("-------------Finally Statement----------------"); System.out.println("try catch finally test end!"); } System.out.println(); //因为在catch语句中有return,所以本语句没有执行 System.out.println("application end."); //因为在catch语句中有return,所以本语句没有执行 /* ///////////////////////////////////////////////////////////////// //测试NullPointerException int ycc; TestException tecc = new TestException(); tecc = null; System.out.println("tecc is " + tecc); String[] ai = new String[5]; System.out.println("ai[1] = " + ai[1]); ///////////////////////////////////////////////////////////////// int yc; TestException tec = new TestException(); tec = null; System.out.println("-------------Exception start location-------------"); yc = tec.i; //用空引用访问非静态成员变量,产生异常 NullPointerException } catch(ArithmeticException e){ System.out.println("ArithmeticException found!"); System.out.println(e.getMessage()); System.out.println("-------------Exception ArithmeticException end location-------------"); return; } catch(NullPointerException e){ System.out.println("NullPointerException found!"); System.out.println(e.getMessage()); System.out.println(); System.out.println(e.toString()); e.printStackTrace(); System.out.println("-------------Exception NullPointerException end location-------------"); return; } finally{ System.out.println(); System.out.println("-------------Finally Statement----------------"); System.out.println("try catch finally test end!"); } System.out.println(); //因为在catch语句中有return,所以本语句没有执行 System.out.println("application end."); //因为在catch语句中有return,所以本语句没有执行 */ }} RuntimeExpetion不需要在code里面显示抛出!而其他异常需要在code里面显示throw! 求教一个正则表达式的问题 Jframe 如何去除下面 标题栏 重载loadclass方法实现了动态类加载 如何处理一个关于路径问题 遇到繁琐的问题! 急:无法使用FtpClient下载 救命啊!Exception in thread "main" java.lang.NoClassDefFoundError calendar.get(Calendar.MONTH)+1 如何Java 调用 PB 进行报表打印 HELP~~!!! javascript的优化问题。急!!! 用java快速导出oracle数据到文本
public Integer(String s)
throws NumberFormatException
NumberFormatException
当应用程序试图将字符串转换成一种数值类型,但该字符串不能转换为适当格式时,抛出该异常.
throws代表调用方法的调用方进行处理,参考..
其次,系统异常是不用你去写代码捕获的,自己写的就需要捕获,可以写try,catch,不写的话,直接在方法或类名的后面申明会抛出什么异常
runtimeExpetion 就是在运行是发生的异常~~比如null指针的异常等,
普通异常就如2楼的,是必要捕获的~要作出相应的处理,
java中编译错误由编译程序javac产生,运行时的错误通常成为(exception),异常是java中的类.
java的异常全部在java.lang.Exception中,RuntimeException是其中一大类,
而ArithmeticException属于此类。整数除零会产生ArithmeticException,
浮点数除零,就不会产生异常,此是程序设计的特点使用java的异常处理机制,只要简单地把可能出现错误的段落包括在try{}中即可,
然后,紧接着try之后,用catch(Exception e)输出错误的情况.
try内的语句如果出现异常,都会产生或者叫throws抛出一个异常对象e给catch。
如果try内的某条语句产生异常,那么程序不会执行其后的try内的语句,而是转向
执行catch内的语句,然后接着执行catch后的语句,程序仍然能够正常结束
提醒: try的意思就是试试,试试其中的语句是否会发生异常,catch的意思是抓住、捕获,
如果有异常发生,就抓住它,并在catch中进行处理。
Throwable类的三种方法:getMessage(),toString(),printStackTrace().
*/public class TestException{
private int i;
static int k;
public static void main(String[] args){
try{
///////////////////////////////////////////////////////////////////
//测试浮点数除零,就不会产生异常
double z = 3; //可以不赋成3.0 但输出成3.0
System.out.println("double z = " + z);
double s = z/0;
System.out.println("double s = " + s); //不产生异常,输出Infinity(无穷大)
/////////////////////////////////////////////////////////////////
//测试整数除零会产生ArithmeticException
int y;
TestException te = new TestException();
te.i = 3; //仍然在同一个类中,可以访问private成员变量i
y = 3/te.i; //产生异常 ArithmeticException: by/zreo
System.out.println("))))))))))))))))))))))))))))))))))");
System.out.println("int y = " + y);
te = null;
System.out.println(te.i);
}catch(ArithmeticException e)
{
System.out.println("ArithmeticException found!");
System.out.println(e.getMessage());
System.out.println("-------------Exception ArithmeticException end location-------------");
return;
}catch(NullPointerException e){
System.out.println("NullPointerException found!");
System.out.println(e.getMessage());
System.out.println();
System.out.println(e.toString());
e.printStackTrace();
System.out.println("-------------Exception NullPointerException end location-------------");
}finally{
System.out.println();
System.out.println("-------------Finally Statement----------------");
System.out.println("try catch finally test end!");
}
System.out.println(); //因为在catch语句中有return,所以本语句没有执行
System.out.println("application end."); //因为在catch语句中有return,所以本语句没有执行
/*
/////////////////////////////////////////////////////////////////
//测试NullPointerException
int ycc;
TestException tecc = new TestException();
tecc = null;
System.out.println("tecc is " + tecc);
String[] ai = new String[5];
System.out.println("ai[1] = " + ai[1]);
/////////////////////////////////////////////////////////////////
int yc;
TestException tec = new TestException();
tec = null;
System.out.println("-------------Exception start location-------------");
yc = tec.i; //用空引用访问非静态成员变量,产生异常 NullPointerException
} catch(ArithmeticException e){
System.out.println("ArithmeticException found!");
System.out.println(e.getMessage());
System.out.println("-------------Exception ArithmeticException end location-------------");
return;
} catch(NullPointerException e){
System.out.println("NullPointerException found!");
System.out.println(e.getMessage());
System.out.println();
System.out.println(e.toString());
e.printStackTrace();
System.out.println("-------------Exception NullPointerException end location-------------");
return;
} finally{
System.out.println();
System.out.println("-------------Finally Statement----------------");
System.out.println("try catch finally test end!");
}
System.out.println(); //因为在catch语句中有return,所以本语句没有执行
System.out.println("application end."); //因为在catch语句中有return,所以本语句没有执行
*/
}
}
而其他异常需要在code里面显示throw!