面向对象(捕获异常try-catch-finally、throws抛异常)

例一:
public class ClassA {
public void methodA(){
System.out.println(“methodA begin…..”);
ClassB.methodB();
System.out.println(“methodA end…..”);
}
public int methodAA(){
//针对一道特有面试题
int result = 0;
try{
result = 5;
return result;//return已经先把5放到某处等待接收,再去执行finally
}catch(Exception ex){
result = 6;
}finally{//必须执行的finally块,return/break/continue都不能阻止finally执行
result = 7;
}
return 0;
}
public void methodAAA(){
Exception ex = new Exception();//产生一个异常对象
System.out.println(“==============”);
}
}

import java.util.InputMismatchException;
public class ClassB {
public static void methodB(){
System.out.println(“methodB begin…..”);
// while(true){
// try{
new ClassC().methodC();
// break;
// }catch(ArithmeticException ae){
// System.out.println(“不能输入0!”);
// }catch(InputMismatchException ime){
// System.out.println(“只能输入整数!”);
// }catch(Exception ex){
// ex.printStackTrace();
// }
// }
System.out.println(“methodB end…..”);
}
}

import java.util.InputMismatchException;
import java.util.Scanner;
public class ClassC {
public void methodC(){
System.out.println(“methodC begin……”);
while(true){
try{
System.out.println(“请输入一个正整数:”);
int input = new Scanner(System.in).nextInt();
int result = 100 / input;
System.out.println(“result = ” + result);
break;
}catch(ArithmeticException ae){
System.out.println(“不能输入0!”);
}catch(InputMismatchException ime){
System.out.println(“只能输入整数!”);
}catch(Exception ex){
ex.printStackTrace();
}
}
System.out.println(“methodC end……”);
// System.out.println(“methodC begin……”);
// System.out.println(“请输入一个正整数:”);
// int input = new Scanner(System.in).nextInt();
// int result = 100 / input;
// System.out.println(“result = ” + result);
// System.out.println(“methodC end……”);
}
}

public class TestMain {
public static void main(String[] args) {
// System.out.println(“main begin…..”);
// ClassA a = new ClassA();
// a.methodA();
// System.out.println(“main end…..”);
// while(true){
// try{
// System.out.println(“请输入一个除数:”);
// int a = new Scanner(System.in).nextInt();
// int result = 100 / a;
// System.out.println(“result = ” + result);
// return;
// }catch(InputMismatchException ime){
// System.out.println(“输入不匹配异常”);
// }catch(ArithmeticException ae){
// System.out.println(“除数不能为0”);
// }finally{
// //往往放在finally当中的都是资源的清理,通道的关闭动作。
// //finally非常强大,不管是break,continue还是return,都无法阻止它被执行
// //finally在它们之前被执行。
// System.out.println(“不管是否发生异常都必须要执行”);
// }
// }
// int result = new ClassA().methodAA();
// System.out.println(result)
// new AClass().aMethod();
// try {
// new AClass().bMethod();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } catch (SQLException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
//自定义异常的使用
GUIClass gc = new GUIClass();
gc.clickButton();
}
}

例二:throw 产生异常对象和 throws抛出的辨析
/*
* throw 和 throws的辨析:
* 1、throw和throws都是关键字,都是属于异常处理部分的;
* 2、throw是写在方法体当中的,后面跟的是一个异常对象;
* throws是写在方法的申明的最后的,后面跟的是一个或多个异常类
* 3、当运行到throw这句代码的时候,就会真正发生抛出一个异常对象的动作;
* throws是在编译期警告本方法的调用者,有可能会发生哪些类型的异常。
* 4、当一个方法体当中有throw语句,不一定会有throws,除非throw后面
* 跟的是编译时异常,那就必须写throws。
*/
public class AClass {
public void aMethod(){
int choice = new Scanner(System.in).nextInt();
if(choice > 5){
//此时只是在内存中产生了一个异常对象,它仅在内存中存在没有纳入到异常传播机制中
//使用throw关键字抛出这个异常对象
throw new IndexOutOfBoundsException();运行时异常不用抛,编译时异常需要throws抛异常
}
}
//一个完整的方法申明:访问修饰符 可选修饰符 返回类型 方法名(形参列表)throws 抛出的异常列表
//throws还会影响到重写的规范—子类重写方法不能抛出比父类被重写方法更多的异常。
// 这里的更多不是指异常类型的个数,而是异常类型的范围。
public void bMethod() throws Exception{
int choice = new Scanner(System.in).nextInt();
if(choice > 5){
//当主动抛出的是一个编译时异常的时候,必须要在编译期对本方法的调用者提出警告
//所以在方法的申明部分,应该有throws内容,表明本方法有可能发生异常。
throw new Exception();
}
}
}

public class BClass extends AClass {
public void bMethod() throws IOException,SQLException{
// TODO Auto-generated method stub
}

}

例三:自定义异常

//1、自定义异常一定要继承Exception
public class LovoException extends Exception{
public LovoException(){
}
//2、自定义异常至少要有两个带参构造;
// 一个带异常参,一个带字符串参。
// 前者用于把其它异常类型转换为自定义异常
// 后者用于操作非JVM主动抛出的业务异常(比如:工资为负)
public LovoException(Exception ex){
super(ex);//调用父类的构造
}
public LovoException(Throwable th){
super(th);
}
public LovoException(String msg){
super(msg);
}
//3、可以给自定义异常提供扩展方法
public void writeLog(){
System.out.println(“打印日誌”);
}
}

//表示层类
public class GUIClass {
//点击按钮
public void clickButton(){
try{
//接收用户输入数据
//输入数据有效性验证
//调用业务类完成业务操作
boolean flag = new ServiceClass().service();
//根据返回结果,跳转新页面或给出新信息
}catch(LovoException le){
le.printStackTrace();
}
}
}

//业务层的类
public class ServiceClass {
public boolean service() throws LovoException{
try{
//完成业务操作
int i = 10 / -5;
if(i < 0){
throw new LovoException(“工资不能为负”);
}
return true;
}catch(Exception ae){
throw new LovoException(ae);
}
}
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值