JAVA Exception

package com.zhengHong.chongZaiClass;

import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.regex.Pattern;

public class Cal {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// String str = input.next();
// String s = input.next();
// int count = 0;
// int index = 0;
str.indexOf(s);返回当前字符串中s字符串出现的位置
// while ((index = str.indexOf(s))>=0){
// count++;
// str = str.substring(index + s.length());
// }
// System.out.println(count);

// System.out.println(“输入一个邮箱地址:”);
// String email = input.next();
// 正则表达式(模板)
// 1、String方法matches(正则表达式)
// String reg = “\w{3,}@\w{3,}.\w{3,}”;//第一个斜杠转义字符
System.out.println(email.matches(reg));
// Pattern p = Pattern.compile(reg);//正则表达式的类
// boolean bool = p.matcher(email).matches();
// System.out.println(bool);

// System.out.println(“判断电话号:”);
// String pn = input.next();
// Pattern p = Pattern.compile(“1[3,4,6,9,8][0-9]{9}”);
// boolean bool = p.matcher(pn).matches();
// System.out.println(bool);

// 出现异常 (运行时出现的问题) 并非编译的语法错误
// try{有可能出现异常的语句} catch(异常){}
// try语句块中出现异常就执行catch不会中断程序(只是try中的异常后面语句不执行)
//异常也分种类
//try 块出现异常 会和catch中的相匹配,如果匹配不上继续中断
//异常不可能同时出现两种

    int result = 0;
    try{
        int num1 = input.nextInt();
        int num2 = input.nextInt();
        result = num1/num2;
    }catch (InputMismatchException e){//此处的e没有赋值,没有new,值为null
        e.printStackTrace();//此处的e的地址为以上try块中异常地址,就是如果没有异常就不执行catch块
        System.out.println(e.getMessage());
        System.out.println("输出语法错误!");
    }catch (ArithmeticException a){
        System.out.println("算法异常");
    }finally {
        System.out.println("finally");
    }
    System.out.println(result);



}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值