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();
  }
 }

 

posted @ 2017-12-15 12:07  水墨&丹青  阅读(88)  评论(0编辑  收藏  举报