异常处理:

来源:1-2 Java中使用try..catch..finally实现异常处理

慕仙6110522

2018-06-18 12:53

输入的是否是整数可以使用判断结构吗?

写回答 关注

3回答

  • 慕丝5139627
    2018-06-18 13:22:37
    已采纳

    Scanner input=new Scanner(System.in);

    if(input.hasNext()) {

         try{

             Integer.valueOf(input.nextLine());#转为int成功的话,就是整数,转化失败就不是整数

             System.out.println("你输入的是整数");

         }

        catch(Exception e){

              System.out.println("你输入的不是整数");

         }

    }


  • 程序猿我为你高歌_0
    2018-09-09 19:07:35


    /**

     * 学习异常

     * @author zhuzg

     *

     */

    public class TestException {

    public static void main(String[] args) {

    /*try {

    int i = 100/0;

    }catch(java.lang.ArithmeticException e){

    e.printStackTrace();

    }

    int[] iArray = new int[10];

    iArray[1] = 10;*/

    //iArray[1] = new Object();

    System.out.println("TestException is " + TestException());

    System.out.println("TestException2 is " + TestException2());

    System.out.println("TestException3 is " + TestException3());

    System.out.println(" testException5   result is " + testException5() ); 

    MyCls mc = new MyCls();

     

    }

    /**

    *   问返回多少   4

    * @return

    */

    public static int  TestException() {

    // TODO Auto-generated constructor stub

    try {

    int i = 100/0;

    return 1;

    }catch(java.lang.ArithmeticException e){

    e.printStackTrace();

    return 2;

    }finally{

    return 4;

    }

    //return 3;

    }

    /**

    *   问返回多少  2

    * @return

    */

    public static int  TestException2() {

    // TODO Auto-generated constructor stub

    try {

    int i = 100/0;

    return 1;

    }catch(java.lang.ArithmeticException e){

    e.printStackTrace();

    return 2;

    }

    //return 3;

    }

    /**

    * 返回内容

    * @return

    */

    public static int  TestException3() {

    // TODO Auto-generated constructor stub

    try {

    int i = 100/0;

    return 1;

    }catch(java.lang.ArithmeticException e){

    //e.printStackTrace();

    int j= 100/0;

    return 2;

    }

    //return 3;

    }

    public static int  TestException4() {

    // TODO Auto-generated constructor stub

        // 错误的代码写法  前边不能包括后边

    /* try {

    int i = 100/0;

    return 1;

    }catch(Exception e) {

    }catch(ArithmeticException e){

    //e.printStackTrace();

    int j= 100/0;

    return 2;

    }*/

    /**

    *  后边可以包含前边

    */

    try {

    int i = 100/0;

    return 1;

    }catch(ArithmeticException e) {

    return 100;

    }catch(Exception e){

    //e.printStackTrace();

    int j= 100/0;

    return 2;

    }

    //return 3;

    }

    /**

    * 不会有任何返回会

    * @return

    */

    public static int testException5() {

    try {

    return 100/0;

    }catch(Exception e) {

    System.exit(10);

    return 10;

    }finally {

    return 20;

    }

    }

    public static int testException6() {

    try {

    return 100/0;

    }catch(Exception e) {

    // 得到的是错误详细信息

    // e.getMessage()

    // 打印栈的调用顺序图

    // e.printStackTrace();

    //  既然是栈  一定在同一个线程     一个线程不可能打印另外一个线程中异常内容。

    // 一个线程中的函数绝对不可能调用到其他线程的函数

    return 10;

    }finally {

    return 20;

    }

    }


    }


    /**

     * 构造器也需要处理异常

     * @author zhuzg

     *

     */

    class MyCls{

    public MyCls() {

    int i = 100/0;

    }

    }


    学霸985

    评论时可以选择代码框输入代码的,,,

    2019-02-22 15:48:07

    共 1 条回复 >

  • qq_Kindergarten_0
    2018-06-27 08:02:56

    你可以使用instanceof关键字来判断是不是整数,例如:https://img2.mukewang.com/5b32d42c00017df606300299.jpg




Java入门第三季

Java中你必须懂得常用技能,不容错过的精彩,快来加入吧

409784 学习 · 4339 问题

查看课程

相似问题