zoukankan      html  css  js  c++  java
  • 2017.11.27T19_B2_7.3yichang

    package com.xdf.demo;

    import java.util.InputMismatchException;
    import java.util.Scanner;

    public class Demo {

     /**
      *    需求:
      *       01.让用户输入两个数字
      *       02.求两个数字的商
      *       03.使用异常处理机制处理上诉例子可能出现的异常
      */
     public static void main(String[] args) {

      Scanner input = new Scanner(System.in);
      try {
       System.out.println("请您输入第一个数字:");
       int num1 = input.nextInt();
       System.out.println("请您输入第二个数字:");
       int num2 = input.nextInt();
       System.out.println("num1/num2的值是====》" + (num1 / num2));
      } catch (InputMismatchException e) {
       System.out.println("出现了  类型不匹配 异常!");
      } catch (ArithmeticException e) {
       System.out.println("出现了  算术 异常!");
      } finally {
       System.out.println("程序结束");
      }

     }****************************************************

    package com.xdf.demo;

    import java.util.InputMismatchException;
    import java.util.Scanner;

    public class Demo {

     /**
      *    需求:
      *       01.让用户输入两个数字
      *       02.求两个数字的商
      *       03.使用异常处理机制处理上诉例子可能出现的异常
      */
     public static void main(String[] args) {

      Scanner input = new Scanner(System.in);
      try {
       System.out.println("请您输入第一个数字:");
       int num1 = input.nextInt();
       System.out.println("请您输入第二个数字:");
       int num2 = input.nextInt();
       System.out.println("num1/num2的值是====》" + (num1 / num2));
      } catch (InputMismatchException e) {
       System.out.println("出现了  类型不匹配 异常!");
      } catch (ArithmeticException e) {
       System.out.println("出现了  算术 异常!");
      } finally {
       System.out.println("程序结束");
      }

     }**********************************************************

    package com.xdf.demo;

    public class ExceptionDemo {

     public static void main(String[] args) {
      System.out.println(1);
      try {
       System.out.println(2 / 0);
       System.exit(1);
      } catch (ArithmeticException e) {
       System.out.println("进入了  catch");
      } finally {
       System.out.println(3);
       System.out.println(4);
       System.out.println(5);
       System.out.println(6);
       System.out.println(7);
       System.out.println(8);
       System.out.println(9);
       System.out.println(10);

    *******************************************************

    package com.xdf.exception;

    public class AgeException extends StudentException {

     public AgeException() {
      super("年龄异常");

    ************************************

    package com.xdf.exception;

    public class AgeException extends StudentException {

     public AgeException() {
      super("年龄异常");

    ***************************************

    package com.xdf.exception;

    import java.util.Scanner;

    public class StudentDemo {

     public static void main(String[] args) throws StudentException {

      Scanner input = new Scanner(System.in);
      System.out.println("请输入异常(age/name)");
      String answer = input.next();
      if (answer.equals("age")) {
       throw new AgeException();
      } else if (answer.equals("name")) {
       throw new NameException();
      } else {
       createStudent();
      }

     }

     private static void createStudent() throws StudentException {
      throw new StudentException("学生创建异常");
     }

    ***********************************************************

    package com.xdf.exception;

    /**
     *
     * 针对于Student的异常类
     */
    public class StudentException extends Exception {

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

     public StudentException(String msg, Throwable e) {
      super(msg, e);
     }

    ********************************************************

    package com.xdf.link;

    import java.io.IOException;
    import java.sql.SQLException;
    import java.util.InputMismatchException;

    public class LinkDemo {

     // main方法
     public static void main(String[] args) {
      try {
       firstException();
      } catch (SQLException e) {
       e.printStackTrace();
      }
     }

     // 第1个异常
     private static void firstException() throws SQLException {
      try {
       secondException();
      } catch (IOException e) {
       e.printStackTrace();
       throw new SQLException("第1个异常", e);
      }
     }

     // 第2个异常
     private static void secondException() throws IOException {
      try {
       thirdException();
      } catch (InputMismatchException e) {
       e.printStackTrace();
       throw new IOException("第2个异常", e);
      }
     }

     // 第3个异常
     private static void thirdException() throws InputMismatchException {
      throw new InputMismatchException("根本的异常");
     }

    *******************************************************************

    package com.xdf.throwdemo;

    public class Student {

     public static void sleep() throws Exception {

      throw new IndexOutOfBoundsException("sleep中的异常信息!"); // 抛出的异常
     }

     public static void main(String[] args) {
      try {
       sleep();
      } catch (Exception e) {
       e.printStackTrace();
      }
     }

  • 相关阅读:
    Beta阶段代码规范与计划
    Alpha总结展望——前事不忘后事之师
    Alpha冲刺成果测试
    Alpha冲刺总结
    码到成功——Beta冲刺随笔 day 5
    码到成功——Beta冲刺随笔 day 4
    码到成功——Beta冲刺随笔 day 3
    码到成功——Beta冲刺随笔 day 2
    码到成功——Beta冲刺随笔 day 1
    项目Beta冲刺(团队)——凡事预则立
  • 原文地址:https://www.cnblogs.com/xiaoxiao1016/p/8042395.html
Copyright © 2011-2022 走看看