设为首页
收藏本站
切换到宽版
登录
立即注册
找回密码
搜索
搜索
本版
帖子
用户
快捷导航
论坛
BBS
VIP用户组
官网群
无名商城论坛
»
论坛
›
资源分享区
›
学习资源专区
›
【LSP】Java异常(三)
返回列表
发帖
查看:
301
|
回复:
0
[其他技术]
【LSP】Java异常(三)
[复制链接]
无名
无名
当前离线
积分
32464
1万
主题
1万
帖子
3万
积分
管理员
积分
32464
发消息
发表于 2022-5-8 17:04:20
|
显示全部楼层
|
阅读模式
五、 自定义异常如何自定义异常类?继承于现有的异常结构:RuntimeException 、Exception提供全局常量:serialVersionUID提供重载的构造器public class MyException extends Exception{ static final long serialVersionUID = -7034897193246939L; public MyException(){ } public MyException(String msg){ super(msg); }}此时使用自己定义的异常类package com.atguigu.java2;public class StudentTest { public static void main(String[] args) { try { Student s = new Student(); s.regist(-1001); System.out.println(s); } catch (Exception e) {// e.printStackTrace(); System.out.println(e.getMessage()); } } }class Student{ private int id; public void regist(int id) throws Exception { if(id > 0){ this.id = id; }else{ throw new MyException("不能输入负数"); } } @Override public String toString() { return "Student [id=" + id + "]"; } }6|0六、问题进入A----用A方法----制造异常----进入方法B-----调用B
public
,
异常
,
MyException
,
Exception
,
Student
相关帖子
•
【HC】JAVA中全局变量
•
【IAPP】裕V5 yc 异常
•
【HC】一个简单的java程序
•
【HC】Java的一些问题以及解决方法
•
【HC】简单理解实咧
•
【HC】转型对象(Java)
•
【HC】【考核】设计模式——解释器模式
•
C++构造函数(Constructor)
•
java輸出hello world練習
•
【FUT】Java代码分享-打印九九乘法表
回复
使用道具
举报
返回列表
发帖
高级模式
B
Color
Image
Link
Quote
Code
Smilies
您需要登录后才可以回帖
登录
|
立即注册
本版积分规则
发表回复
回帖后跳转到最后一页
快速回复
返回顶部
返回列表