异常--throw
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace @throw
{
class test
{
public int Myint(string a, string b)
{
int intn1;
int intn2;
int num;
intn1 = int.Parse(a);
intn2 = int.Parse(b);
if(intn2 == 0)
{
throw new DivideByZeroException();
return 0;
}
else
{
num = intn1 / intn2;
return num;
}
}
} class Program
{
static void Main(string[] args)
{
while(true)
{
Console.WriteLine("请输入分子:");
string str1 = Console.ReadLine();
Console.WriteLine("请输入分母:");
string str2 = Console.ReadLine();
test tt = new test();
Console.WriteLine(tt.Myint(str1,str2));
}
}
}
}
throw在特定情况下主动抛出异常
(1)ArithmeticException
(2)ArrayTypeMismatchException
(3)DivideByZeroException
(4)IndexOutOfRangeException
(5)InvalidCastException
(6)NullReferenceException
(7)OutOfMemoryException
(8)OverflowException
(9)StackOverflowException
(10)TypeInitializationException
异常--throw的更多相关文章
- 异常-throw的概述以及和throws的区别
package cn.itcast_06; /* * throw:如果出现了异常情况,我们可以把该异常抛出,这个时候的抛出的应该是异常的对象. * * throws和throw的区别(面试题) thr ...
- Java中处理异常throw和throws
1.首先我们来了解什么是异常呢? 异常阻止当前方法或作用域继续执行的问题. 2.处理异常 说到处理异常,我们当然会想到 try catch finally 在java中我们会对异常的处理有更高的认识 ...
- JAVA 异常 throw 与 throws
最近一直throw和throw new …… 获取头部罢工,要彻底生气清楚这件事,他对这个思想精华收集了很多网友.这里摘录. throws全部异常信息throw则是指抛出的一个详细的异常类型.通常在一 ...
- java为什么有些异常throw出去需要在函数头用throws声明,一些就不用。
Excepiton分两类:checked exception.runtime exception:直接继承自Exception就是checked exception,继承自RuntimeExcepti ...
- JS Error 内置异常类型 处理异常 Throw语句
Exceptional Exception Handling in JavaScript MDN资料 Anything that can go wrong, will go wrong. ...
- 【Java】异常 —— throw, throws, try catch 相关内容
嗯……面试考到了这个,又是一个如无意外 那么接下来就总结吧 一.什么是异常 程序运行过程中发生的异常事件. RuntimeException通常是因为编程员因为疏忽没有检查而引起的错误. 二.Exce ...
- 为什么有些异常throw出去需要在函数头用throws声明,一些就不用
throw new IllegalStateException(".");不用在函数头声明throws IllegalStateExceptionthrow new IOExcep ...
- JAVA笔记9__异常/throw关键字/自定义异常/受检与非受检异常、assert关键字/StringBuffer、StringBuilder/代码国际化、动态文本
/** * 异常:在程序中导致程序中断运行的一些指令 * 1.受检异常:编译期 * 2.非受检异常:运行期 * 异常处理过程分析: * 1.一旦产生异常,系统会自动产生一个异常类的实例化对象 * 2. ...
- 抛异常 throw的注意事项
子类覆盖父类只能抛出父类的异常或者子类或者子集注意:如果父类的方法没有抛异常,那么子类覆盖时绝对不能抛. 子类继承父类时,方法抛异常,要么抛父类,要么抛父类下的子类,不能抛父类平级或以上的异常 原因是 ...
- java 异常 throw
throw UnsupportedOperationException(); //没有支持的操作NoSuchElementException(); //没有这样的元素
随机推荐
- Rsync+inotify实现文件实时同步#附shell脚本
强烈推荐先仔细看此文 https://segmentfault.com/a/1190000002427568 实验环境 centos 7.3 vm2:192.168.221.128 同步服务器 vm1 ...
- 在window10平台下安装TensorFlow(only cpu)
这是我在安装tensorflow遇到的问题记录 希望可以给大家一些帮助(2019年1月6日) 1. 需要安装的环境及软件 python3.6 Anaconda Tensorflow 2. 先安装ana ...
- redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: connect time out
redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: connect ti ...
- 【转】odoo装饰器:model
model装饰器的作用是返回一个集合列表,一般用来定义自动化动作里面,该方法无ids传入. 应用举例: 定义columns langs = fields.Selection(string=" ...
- BZOJ1029_建筑抢修_KEY
题目传送门 这是一道贪心的问题. 总体做法是这样的:先按照报废的快慢从小到大SORT一遍,优先修报废快的.同时开一个大根堆(C++的朋友可以用priority_queue),用来记录已经修了的建筑的耗 ...
- MySQL 存储过程常用SQL语句收集
1,select curdate() /*2016-10-08*/ 2,select date_sub(curdate(), INTERVAL 6 DAY) /*2016-10-02*/ 3,case ...
- Please ensure JDK installation is valid and compatible with the current OS
报错如下: Gradle sync failed: Could not run JVM from the selected JDK. Please ensure JDK installation is ...
- Ruby on Rails Tutorial 第2版 学习笔记
Ruby on Rails Tutorial 第2版 在线阅读:http://railstutorial-china.org/ 英文版:http://ruby.railstutorial.org/ru ...
- 抽样分布(3) F分布
定义 设U~χ2(n1), V~χ2(n2),且U,V相互独立,则称随机变量 服从自由度为(n1,n2)的F分布,记为F~F(n1,n2),其中n1叫做第一自由度,n2叫做第二自由度. F分布的概率密 ...
- EF Core注意事项
流程:https://docs.microsoft.com/en-us/ef/core/get-started/aspnetcore/new-db 1.Both Entity Framework 6. ...