使用throw和throws 引发异常
1.throw 用在方法内抛出异常,通常可以自行使用try catch进行异常处理
如果不自行处理的话,需要在方法上使用throws抛出异常
public static void testAge(){
System.out.println("请输入年龄:");
Scanner input =new Scanner(System.in);
int age=input.nextInt();
try {
if (age<18||age>80){
throw new Exception("18岁一下,80岁以上的住客必须陪同");
}else {
System.out.println("欢迎光临本酒店");
}
}catch (Exception e){
log.error("年龄异常");
}
}
调用方法处理一场
public static void main(String[] args) {
try {
testAge();
} catch (Exception e) {
e.printStackTrace();
log.error("年龄异常");
}
}
public static void testAge() throws Exception{
System.out.println("请输入年龄:");
Scanner input =new Scanner(System.in);
int age=input.nextInt();
if (age<18||age>80){
throw new Exception("18岁一下,80岁以上的住客必须陪同");
}else {
System.out.println("欢迎光临本酒店");
}
}
抛出运行时异常
抛出运行时异场,不需要处理,但是程序或中断运行
public static void testAge(){
System.out.println("请输入年龄:");
Scanner input =new Scanner(System.in);1
int age=input.nextInt();
if (age<18||age>80){
throw new RuntimeException("18岁一下,80岁以上的住客必须陪同");
}else {
System.out.println("欢迎光临本酒店");
}
System.out.println("iver");
}
使用throw和throws 引发异常的更多相关文章
- Java基础系列--throw、throws关键字
原创作品,可以转载,但是请标注出处地址:http://www.cnblogs.com/V1haoge/p/8484697.html 一.概述 throw和throws就是异常相关的关键字,在java中 ...
- JAVA 异常 throw 与 throws
最近一直throw和throw new …… 获取头部罢工,要彻底生气清楚这件事,他对这个思想精华收集了很多网友.这里摘录. throws全部异常信息throw则是指抛出的一个详细的异常类型.通常在一 ...
- JAVA之旅(十)——异常的概述,Try-Catch,异常声明Throws,多异常处理,自定义异常,Throw和Throws的区别
JAVA之旅(十)--异常的概述,Try-Catch,异常声明Throws,多异常处理,自定义异常,Throw和Throws的区别 不知不觉,JAVA之旅这个系列已经更新到第十篇了,感觉如梦如幻,时间 ...
- C#中异常:“The type initializer to throw an exception(类型初始值设定项引发异常)”的简单分析与解决方法
对于C#中异常:“The type initializer to throw an exception(类型初始值设定项引发异常)”的简单分析,目前本人分析两种情况,如下: 情况一: 借鉴麒麟.NET ...
- Java异常之try,catch,finally,throw,throws
Java异常之try,catch,finally,throw,throws 你能区分异常和错误吗? 我们每天上班,正常情况下可能30分钟就能到达.但是由于车多,人多,道路拥挤,致使我们要花费更多地时间 ...
- 【Java学习笔记之三十二】浅谈Java中throw与throws的用法及异常抛出处理机制剖析
异常处理机制 异常处理是对可能出现的异常进行处理,以防止程序遇到异常时被卡死,处于一直等待,或死循环. 异常有两个过程,一个是抛出异常:一个是捕捉异常. 抛出异常 抛出异常有三种形式,一是throw, ...
- java异常——五个关键字(try、catch、finally、throw、throws)
一.try.catch.finally常用组合 try{ xxx }catch(xxxException e){ e.printStackTrace(); } try{ xxx }catch(xxxE ...
- Java -- 异常的捕获及处理 -- 范例 -- throw与throws的应用
7.2.3 范例 -- throw与throws的应用 例:综合应用 Class : Math package limeThrowable._7_2_3; public class Math { pu ...
- “全栈2019”Java异常第十章:throw与throws区别详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java异 ...
随机推荐
- Tomcat - Tomcat安装
Tomcat官网:http://tomcat.apache.org/ 准备:JAVA环境布置完成 一.Windows平台 1. 版本选择 1) 进入官网 2) 查看版本匹配 官网说明 https:// ...
- git -C <other-git-repo-path> [git-command] 指定其它路径的仓库 执行命令
git -C <other-git-repo-path> [git-command] 指定其它路径的仓库 执行命令 注意,-C 要在命令之前 例如: git -C d:/testRepo ...
- Spring入门篇——第2章 Spring IOC容器
第2章 Spring IOC容器 介绍Spring IOC容器的基本概念和应用 2-1 IOC及Bean容器 自己的理解:什么是IOC?就是利用配置文件(外部容器)来创建对象. 在IOC容器中,所有对 ...
- Codeforces Round #589 (Div. 2) D. Complete Tripartite(染色)
链接: https://codeforces.com/contest/1228/problem/D 题意: You have a simple undirected graph consisting ...
- @Configuration的使用 和作用(转)
从Spring3.0,@Configuration用于定义配置类,可替换xml配置文件,被注解的类内部包含有一个或多个被@Bean注解的方法,这些方法将会被AnnotationConfigApplic ...
- oracle split函数
PL/SQL 中没有split函数,需要自己写. 代码: ); --创建一个 type ,如果为了使split函数具有通用性,请将其size 设大些. --创建function create or r ...
- [Algorithm] Find The Vowels
// --- Directions // Write a function that returns the number of vowels // used in a string. Vowels ...
- 基于ElementUI封装可复用的表格组件
<template> <section class="ces-table-page"> <!-- 表格操作按钮 --> <section ...
- compile cmdow
1. http://www.commandline.co.uk/ https://github.com/ritchielawrence/cmdow/tarball/master 2. g++ -O2 ...
- effective c++ (四)
条款10:令operator=返回一个reference to *this 为了实现“连锁赋值”,赋值操作符必须返回一个reference指向操作符的左侧实参,这是你为classes实现赋值操作符时应 ...