zoukankan      html  css  js  c++  java
  • 异常处理

    java.lang.Throwable

    Error:错误 程序中不进行处理

    Exception:异常,要求在编写程序时,就要考虑到这些异常的处理。

        编译时异常 或运行时异常。

    解决方法:1.遇到错误就终止程序的运行。

    2.错误的检测,错误消息的提示,以及错误的处理。

     异常处理五个关键字:

    try catch finally throws throw 

    处理异常:

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.Date;
    import java.util.InputMismatchException;
    import java.util.Scanner;

    import org.junit.Test;

    /*
    * 二、如何处理Exception的异常
    * Java提供的是异常处理的抓抛模型
    * 1."抛":当我们执行代码时,一旦出现异常,就会在异常的代码处生成一个对应的异常类型的对象,并
    * 将此对象抛出。(自动抛出 / 手动抛出)
    * >一旦抛出此异常类的对象,那么程序就终止执行
    * >此异常类的对象抛给方法的调用者。
    * 2."抓":抓住上一步抛出来的异常类的对象。如何抓?即为异常处理的方式
    * java 提供了两种方式用来处理一个异常类的对象。
    * 处理的方式一:
    * try{
    * //可能出现异常的代码
    * }catch(Exception1 e1){
    * //处理的方式1
    * }catch(Exception2 e2){
    * //处理的方式2
    * }finally{
    * //一定要执行的代码
    * }
    * 注:1.try内声明的变量,类似于局部变量,出了try{}语句,就不能被调用
    * 2.finally是可选的。
    * 3.catch语句内部是对异常对象的处理:
    * >getMessage(); printStackTrace();
    * 4.可以有多个catch语句,try中抛出的异常类对象从上往下去匹配catch中的异常类的类型,一旦满足
    * 就执行catch中的代码。执行完,就跳出其后的多条catch语句
    * 5.如果异常处理了,那么其后的代码继续执行。
    * 6.若catch中多个异常类型是"并列"关系,孰上孰下都可以。
    * 若catch中多个异常类型是"包含"关系,须将子类放在父类的上面,进行处理。否则报错!
    * 7.finally中存放的是一定会被执行的代码,不管try中、catch中是否仍有异常未被处理,以及是否有return语句。
    * 8.try-catch是可以嵌套的。
    *
    * 三、对于运行时异常来说,可以不显式的进行处理。
    * 对于编译时异常来说,必须要显式的进行处理。
    */
    public class TestException1 {
    // 编译时异常
    @Test
    public void test6() {
    FileInputStream fis = null;
    try {
    fis = new FileInputStream(new File("hello.txt"));
    int b;
    while ((b = fis.read()) != -1) {
    System.out.print((char) b);
    }

    } catch (FileNotFoundException e1) {
    System.out.println("文件找不到了!");
    } catch (IOException e1) {
    System.out.println(e1.getMessage());
    } finally {
    try {
    fis.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }

    // 常见的运行时异常
    // 4.空指针异常:NullPointerExcetion
    @Test
    public void test5() {
    // Person p = new Person();
    // p = null;
    // System.out.println(p.toString());

    try {
    String str = new String("AA");
    str = null;
    System.out.println(str.length());
    } catch (Exception e) {
    // e.printStackTrace();
    System.out.println("出现空指针的异常了");
    }
    }

    // 3.类型转换异常:ClassCastException
    @Test
    public void test4() {
    try {
    Object obj = new Date();
    String str = (String) obj;
    } catch (ClassCastException e) {
    System.out.println("出现类型转换的异常了");
    //System.out.println(10 / 0);
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    System.out.println("hello!美女!");
    }
    // String str1 = (String)new Date();
    }

    // 2.算术异常:ArithmeticException
    @Test
    public void test3() {
    try {
    int i = 10;
    System.out.println(i / 0);
    } catch (Exception e) {
    // e.printStackTrace();
    System.out.println(e.getMessage());
    }
    }

    // 1.数组下标越界的异常:ArrayIndexOutOfBoundsException
    @Test
    public void test2() {
    try {
    int[] i = new int[10];
    // System.out.println(i[10]);
    System.out.println(i[-10]);
    } catch (Exception e) {
    System.out.println("出现异常了!");
    }
    }

    @Test
    public void test1() {
    Scanner s = new Scanner(System.in);
    try {
    int i = s.nextInt();
    System.out.println(i);
    } catch (InputMismatchException e) {
    System.out.println("出现类型不匹配的异常了!");
    }
    }
    }


    /*
    * 异常处理的方式二:在方法的声明处,显式的抛出该异常对象的类型
    * 格式:如:public static void method2() throws FileNotFoundException,IOException{}
    * 当在此方法内部出现异常的时候,会抛出一个异常类的对象,抛给方法的调用者。
    * 异常的对象可以逐层向上抛,直至main中。当然在向上抛的过程中,可以再通过try-catch-finally进行处理。
    *
    * java的异常处理:抓抛模型
    * 1.抓:异常的处理,有两种方式(①try-catch-finally② throws + 异常的类型)
    * 2.抛:一旦执行过程中,出现异常,会抛出一个异常类的对象。(自动的抛出 vs 手动的抛出(throw + 异常类的对象))
    * >异常类,既可以是现成的异常类,也可以是自己创建的异常类
    */


    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    public class TestException {

    public static void main(String[] args) {
    try
    {
    mothod1();
    }catch(FileNotFoundException e)
    {
    System.out.println(e.getMessage());
    }catch(IOException e)
    {
    e.printStackTrace();
    }
    }

    public static void mothod1() throws FileNotFoundException, IOException{
    mothod();
    }
    public static void mothod() throws FileNotFoundException,IOException
    {
    FileInputStream fis=new FileInputStream(new File("hello1.txt"));
    int b;
    while((b=fis.read())!=-1)
    {
    System.out.println((char)b);
    }
    fis.close();
    }
    }

    //1.手动的抛出一个异常的例子
    //2.抛出的异常类型:若是RuntimeException,可以不显式的处理
    //若是一个Exception,必须要显式的处理。
    public class TestCircle {
    public static void main(String[] args) {
    Circle c1 = new Circle(2.1);
    Circle c2 = new Circle(2.1);
    try {
    System.out.println(c1.compareTo(c2));
    System.out.println(c1.compareTo(new String("AA")));
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
    class Circle{
    private double radius;

    public double getRadius() {
    return radius;
    }

    public void setRadius(double radius) {
    this.radius = radius;
    }

    public Circle(double radius) {
    super();
    this.radius = radius;
    }
    //比较两个圆的半径的大小。
    public int compareTo(Object obj) throws Exception{
    if(this == obj){
    return 0;
    }
    else if(obj instanceof Circle){
    Circle c = (Circle)obj;
    if(this.radius > c.radius){
    return 1;
    }else if(this.radius == c.radius){
    return 0;
    }else{
    return -1;
    }
    }else{
    //return -2;
    //手动的抛出一个异常
    throw new Exception("传入的类型有误!");

    }
    }

    //如何自定义一个异常类
    //1.自定义的异常类继承现有的异常类
    //2.提供一个序列号,提供几个重载的构造器
    public class MyException extends Exception{

    static final long serialVersionUID = -70348975766939L;

    public MyException(){

    }
    public MyException(String msg){
    super(msg);
    }
    }

    用自己创建的异常类:

    public class TestStudent {
    public static void main(String[] args) {
    Student s = new Student();
    try {
    s.regist(-12);
    System.out.println(s);
    } catch (MyException e) {
    System.out.println(e.getMessage());
    }
    }
    }

    class Student {
    int id;

    public void regist(int id) throws MyException {
    if (id > 0) {
    this.id = id;
    } else {
    throw new MyException("传入的学号有误!");
    }
    }

    @Override
    public String toString() {
    return "Student [id=" + id + "]";
    }

    }

    //子类重写的父类的方法,其抛出的异常类型只能是被重写的方法的异常类的子类或异常类型一样。


    public class ReturnExceptionDemo {
    static void methodA() {
    try {
    System.out.println("进入方法A");
    throw new RuntimeException("制造异常");
    } finally {
    System.out.println("用A方法的finally");
    }}
    static int methodB() {
    try {
    System.out.println("进入方法B");
    return 1;
    } finally {
    System.out.println("调用B方法的finally");
    return 2;
    }}
    public static void main(String[] args) {
    try {
    methodA();
    } catch (Exception e) {
    System.out.println(e.getMessage());
    }
    int i = methodB();
    System.out.println(i);
    }
    }

    执行结果:

    进入方法A

    用A方法的finally

    制造异常

    进入方法B

    调用B方法的finally

    2

  • 相关阅读:
    通达OA二次开发
    通达OA 工作流程 表单设计 高级应用
    PLSQL Develop PlugIn 之脚本自动匹配补全工具CnPlugin
    Oracle性能优化之SQL语句
    Linux重置root密码步骤
    ORA-03113:通信通道的文件结尾解决
    Bat脚本实现MySQL数据库SQL文件备份
    访问父级组件实例
    访问根实例
    插槽其它示例
  • 原文地址:https://www.cnblogs.com/steel-chen/p/6859016.html
Copyright © 2011-2022 走看看