黑马程序员----Java的异常处理机制
2015-10-11 09:42
483 查看
/**
* @author Administrator
*
* @description 异常学习测试类
* @history
*/
public class ExceptionDemo {
/**
*@description
*@param args
*/
public static void main(String[] args) {
// Throable类是所有错误和异常的根基类
// Throable类下两个重要的子类Exception和Error
// 1、编写一个常见的异常例子
try {
int i = 1;
int j = 0;
int r = i / j;
} catch (ArithmeticException ae) {
ae.printStackTrace();
// Exception in thread "main" java.lang.ArithmeticException: / by zero
}
// 2、 catch多个种类的异常例子
String s1 = "1";
String s2 = "0"; // String s2 = "eclipse";
try{
int i1 = Integer.parseInt(s1); // 字符串解析成数字
int i2 = Integer.parseInt(s2);
int temp = i1/i2;
} catch(ArithmeticException ae){
ae.printStackTrace(); // 分母为0异常捕获
} catch(NumberFormatException nfe){
nfe.printStackTrace(); // 数字格式转换异常捕获
}
// 3、异常可以往上抛出去例子
int[] array = new int[5];
try{
int i3 = array[5]; // 数组越界异常
} catch(ArrayIndexOutOfBoundsException aiobe){
// 捕获异常后往外抛出
// 在某一层统一处理掉这些异常,比如可以采用拦截器
throw aiobe;
}
// 4、自定义异常类,继承自Exception类
try{
// 实例化内部类对象比较特殊点
ExceptionDemo out = new ExceptionDemo(); // 先实例化外部类对象
throw out.new MyException("hello-exception"); // 再实例化内部类对象
} catch(MyException me){
// hello-exception
System.out.println(me.getLocalizedMessage());
}
}
// 自定义异常类
class MyException extends Exception{
public MyException(String msg){
super(msg);
}
}
}
* @author Administrator
*
* @description 异常学习测试类
* @history
*/
public class ExceptionDemo {
/**
*@description
*@param args
*/
public static void main(String[] args) {
// Throable类是所有错误和异常的根基类
// Throable类下两个重要的子类Exception和Error
// 1、编写一个常见的异常例子
try {
int i = 1;
int j = 0;
int r = i / j;
} catch (ArithmeticException ae) {
ae.printStackTrace();
// Exception in thread "main" java.lang.ArithmeticException: / by zero
}
// 2、 catch多个种类的异常例子
String s1 = "1";
String s2 = "0"; // String s2 = "eclipse";
try{
int i1 = Integer.parseInt(s1); // 字符串解析成数字
int i2 = Integer.parseInt(s2);
int temp = i1/i2;
} catch(ArithmeticException ae){
ae.printStackTrace(); // 分母为0异常捕获
} catch(NumberFormatException nfe){
nfe.printStackTrace(); // 数字格式转换异常捕获
}
// 3、异常可以往上抛出去例子
int[] array = new int[5];
try{
int i3 = array[5]; // 数组越界异常
} catch(ArrayIndexOutOfBoundsException aiobe){
// 捕获异常后往外抛出
// 在某一层统一处理掉这些异常,比如可以采用拦截器
throw aiobe;
}
// 4、自定义异常类,继承自Exception类
try{
// 实例化内部类对象比较特殊点
ExceptionDemo out = new ExceptionDemo(); // 先实例化外部类对象
throw out.new MyException("hello-exception"); // 再实例化内部类对象
} catch(MyException me){
// hello-exception
System.out.println(me.getLocalizedMessage());
}
}
// 自定义异常类
class MyException extends Exception{
public MyException(String msg){
super(msg);
}
}
}
相关文章推荐
- 黑马程序员-------接口和抽象类
- 黑马程序员------面向对象继承
- java面试题
- 面试笔试题知识点回顾
- 面试题——存储过程和函数的区别
- 面试题8:输入一个递增排序的数组的一个旋转,输出旋转数组的最小元素。
- Weak、Strong、assign 和 autorelease + 1道面试题
- 黑马程序员——一些常用类的代码实践
- 黑马程序员——Java基础---String类和基本数据类型对象包装类
- 黑马程序员——hi,你好 ;hi,未来
- 【SQL】今天的一个sql面试题(分层函数使用)
- 黑马程序员_JAVA之面向对象(封装,继承和多态)
- 黑马程序员——JavaSE之IO流二
- 黑马程序员——OC基础---协议
- 程序员的思维(转载于伯乐在线-author:水流年)
- 黑马程序员—Foundation框架之NSFileManager、NSNumber与NSDate
- 前端面试记录 阿里云前端 2015-10-10
- 一道关于排序算法的java面试题
- 软件测试面试相关问题
- 十道海量数据处理面试题与十个方法大总结