练手:异常的种种问题
来源:互联网 发布:js splice 数组 编辑:程序博客网 时间:2024/05/17 02:41
/*
* throw和throws有什么区别?
* try、catch、finally分别在什么情况下使用?
* try-catch语句块用来捕获异常
* try语句块只能有一个,而catch则可以有多个,catch必须紧跟try语句后,
* 中间不能有其他任何代码.
* finally,它规定的语句块无论如何都要执行,在一个try-catch中只能有一个finally.
*/
public class Test6 {
/*
*
*
*java使用throws语法声明当前方法有可能引起一个多多个异常;
*使用语法:返回类型 方法名(参数列表)throws 异常类名1,异常类名2,异常类名3;
* throw适用于异常的再次抛出.
* 异常的再次抛出是指昂捕获到异常的时候并不对它直接处理而是把它抛出留给上一层的调用来处理.
*
*
* */
static void method()throws NullPointerException,
IndexOutOfBoundsException,ClassNotFoundException{
String str = null;//设置字符串的内容为null
int strLength = 0;//初始化字符串的长度赋值为0
strLength = str.length();//获取字符串的长度
System.out.println(strLength);
}
static void methodThrow()throws ClassNotFoundException{
try{
Class.forName("");
}catch(ClassNotFoundException e){
System.out.println("方法中把异常再次抛出");
throw e;
}
}
public static void main(String[] args) {
try{
method();
}catch(NullPointerException e){
System.out.println("NullPointerException异常");
e.printStackTrace();
}catch(IndexOutOfBoundsException e){
System.out.println("IndexOutOfBoundsException异常");
e.printStackTrace();
}catch(ClassNotFoundException e){
System.out.println("ClassNotFoundException异常");
e.printStackTrace();
}
System.out.println("-----------------");
try{
methodThrow();
}catch(ClassNotFoundException e){
System.out.println("主方法对异常进行处理");
}
}
}
* throw和throws有什么区别?
* try、catch、finally分别在什么情况下使用?
* try-catch语句块用来捕获异常
* try语句块只能有一个,而catch则可以有多个,catch必须紧跟try语句后,
* 中间不能有其他任何代码.
* finally,它规定的语句块无论如何都要执行,在一个try-catch中只能有一个finally.
*/
public class Test6 {
/*
*
*
*java使用throws语法声明当前方法有可能引起一个多多个异常;
*使用语法:返回类型 方法名(参数列表)throws 异常类名1,异常类名2,异常类名3;
* throw适用于异常的再次抛出.
* 异常的再次抛出是指昂捕获到异常的时候并不对它直接处理而是把它抛出留给上一层的调用来处理.
*
*
* */
static void method()throws NullPointerException,
IndexOutOfBoundsException,ClassNotFoundException{
String str = null;//设置字符串的内容为null
int strLength = 0;//初始化字符串的长度赋值为0
strLength = str.length();//获取字符串的长度
System.out.println(strLength);
}
static void methodThrow()throws ClassNotFoundException{
try{
Class.forName("");
}catch(ClassNotFoundException e){
System.out.println("方法中把异常再次抛出");
throw e;
}
}
public static void main(String[] args) {
try{
method();
}catch(NullPointerException e){
System.out.println("NullPointerException异常");
e.printStackTrace();
}catch(IndexOutOfBoundsException e){
System.out.println("IndexOutOfBoundsException异常");
e.printStackTrace();
}catch(ClassNotFoundException e){
System.out.println("ClassNotFoundException异常");
e.printStackTrace();
}
System.out.println("-----------------");
try{
methodThrow();
}catch(ClassNotFoundException e){
System.out.println("主方法对异常进行处理");
}
}
}
0 0
- 练手:异常的种种问题
- 关于tomcat的种种问题,异常,出错
- 使用wxWidgets的种种问题
- apt-get 的种种问题
- 练手项目的小问题
- .net中连接SYBASE的种种问题
- 大学生学习java的种种问题
- 关于postgresql的种种恶心问题
- 遇到的种种问题1602 24C02
- Thinkpad T440p安装Linux的种种问题
- Ubuntu下搜狗输入法的种种问题
- 安装vmware遇到的种种问题
- 做项目遇到的种种问题
- Tomcat发布Maven项目遇到的种种异常
- 对最近百度数据库异常的种种猜测
- Tomcat发布Maven项目遇到的种种异常
- #python#异常处理练手
- TCP之种种连接异常
- javascript中的类
- 计算机学科国际会议排名
- POJ 2451 Uyuw's Concert(半平面交求面积)
- 业务流程图与数据流图的对比
- JAVA设计模式之 简单工厂模式【Simple Factory Pattern】
- 练手:异常的种种问题
- 2014深信服面经
- UVa 10340 - All in All
- HDU 4288-Coder(模拟)
- Java中对象输出流问题
- Android WifiDisplay分析一:相关Service的启动
- LINQ之路 1: LINQ介绍
- 第一次写文章
- Android Widget所占单元格计算