自定义Exception
本文改编自http://blog.csdn.net/stellaah/article/details/6738424
[总结]
1.自定义异常:
class 异常类名 extends Exception {
public 异常类名(String msg) {
super(msg);
}
}
2.标识可能抛出的异常:
throws 异常类名1,异常类名2
3.捕获异常:
try{} catch(异常类名 y){} catch(异常类名 y){}
4.方法解释:
getMessage() //输出异常的信息
printStackTrace() //输出导致异常更为详细的信息
[代码]
// 自定义异常
class ZeroException extends Exception {
public ZeroException(String msg) {
super(msg);
}
} class NegtiveException extends Exception {
public NegtiveException(String msg) {
super(msg);
}
}
// 自定义异常 End class Calculate {
public int shang(int x, int y) throws ZeroException,NegtiveException {
if (y < 0) {
throw new NegtiveException("您输入的是" + y + ",规定除数不能为负数!");// 抛出异常
}
if (y == 0) {
throw new ZeroException("您输入的是" + y + ",除数不能为0!");
} int m = x / y;
return m;
}
} // main
public class AppTest {
public static void main(String[] args) {
Calculate calculate = new Calculate();
// 捕获异常
try {
System.out.println("商=" + calculate.shang(1, -3));
} catch (ZeroException e) {
System.out.println(e.getMessage());
e.printStackTrace();
} catch (NegtiveException e) {
System.out.println(e.getMessage());
}
}
}
自定义Exception的更多相关文章
- 自定义Exception:MVC抛出自定义异常,并以Json方式返回
相关链接 优点: 可以统一处理所有页面的异常,对所有需要返回json数据的异常,都用同样的方法throw new DVMException().页面展示,controller的错误处理方式一样 节省编 ...
- Java自定义Exception
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- Abp中自定义Exception的HttpStatusCode
Abp中在新版本中,抛出的异常(比如:UserFriendlyException)通过AjaxResponse封装后返回的时候,HttpStatusCode默认指定成了500. 对于一些默认封装好的处 ...
- 自定义Exception异常
自定义异常构建 首先写一个自定义异常,继承Exception,代码如下 public class NoMappingParamString extends Exception { /*无参构造函数*/ ...
- Django rest framework 自定义Exception
使用Dango rest framework时,有时需要raise APIException到前端,为了统一错误返回格式,我们需要对exception的格式进行调整. 方法: 1. 在project/ ...
- 自定义Exception——实战篇
public class EntityConfigurationException : Exception { public EntityConfigurationException(string m ...
- JAVA入门[17]-ControllerAdvice处理exception
1.关于@ControllerAdvice @ControllerAdvice注解本身已经使用了@Component,因此@ControllerAdvice注解所标注的类将会自动被组件扫描获取到,就像 ...
- [SpringMVC]自定义注解实现控制器访问次数限制
我们需要根据IP去限制用户单位时间的访问次数,防止刷手机验证码,屏蔽注册机等,使用注解就非常灵活了 1 定义注解 @Retention(RetentionPolicy.RUNTIME) @Target ...
- 【SpringBoot】单元测试进阶实战、自定义异常处理、t部署war项目到tomcat9和启动原理讲解
========================4.Springboot2.0单元测试进阶实战和自定义异常处理 ============================== 1.@SpringBoot ...
随机推荐
- thinkphp 对数据库的操作
查看ThinkPHP完全开发手册3.1 首先编辑配置文件 thinkphp这个数据库就不乱改了 昨天新建了一个 confluence(utf8)数据库 所以就用它学习一下吧,因为就只建立了一个数据库, ...
- MEF 松耦合
MEF天生就有解耦合的特性,虽然它不是为解耦而生,而主要是为插件类应用的开发而设计.如果主要是为了解除耦合的话可以使用IoC,Unity等. Unity 微软的IOC 代码: using System ...
- 解决:CWnd::SetWindowText报Assertion failure
参考资料: http://www.cnblogs.com/tiancun/p/3756581.html http://www.tc5u.com/mfc/2120698.htm http://forum ...
- ASP.NET MVC Error
Error Handler http://prideparrot.com/blog/archive/2012/5/exception_handling_in_asp_net_mvc http://ww ...
- 算法-KMP串匹配
字符串匹配 http://www.cnblogs.com/jingmoxukong/p/4343770.html 模式匹配是数据结构中字符串的一种基本运算,给定一个子串,要求在某个字符串中找出与该子串 ...
- iOS6:在你的企业系统中支持Passbook
前言 这是一篇翻译,感谢Jonathan Tang. 原文地址:iOS 6 Tutorial: Supporting Passbook within Your Enterprise Systems 正 ...
- Jenkins自动构建
Jenkins is an award-winning, cross-platform, continuous integration and continuous delivery applicat ...
- 网易免费邮件开启smtp教程
网易免费邮件开启smtp教程 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 我们在部署zabbix邮件报警中可能会用到163.com.那么我们如何去开启smtp服务呢? 1 ...
- 遍历Map集合的方法
创建一个MAP的栗子: Map<String, Integer> tempMap = new HashMap<String, Integer>(); tempMap.put(& ...
- [转] JAVA多线程和并发基础面试问答
JAVA多线程和并发基础面试问答 原文链接:http://ifeve.com/java-multi-threading-concurrency-interview-questions-with-ans ...