Java 自定义异常解读
来源:互联网 发布:苹果手机截动图软件 编辑:程序博客网 时间:2024/06/01 20:03
Java自定义异常习题
7-1:编写一个异常类MyException,在编写一个Student类,该类有一个产生异常的方法,speak(int m),要求参数大于1000时方法抛出这个异常,编写主类,在主类中创建对象调用。
7-3:创建Computer类,该类中有一个计算两个数的最大公约数的方法,如果向该方法传递负整数就抛出异常。
案例基于java程序设计慕课版的第七章练习题(Page 129)为背景。
1:首先来回顾一下基础知识点:
throw 是用于方法内部抛出异常的,throws 是定义方法时这个方法要抛出的异常,这个异常不想捕获就一直向上抛,直到方法的调用者捕获这个异常。
throw new MyException("“说出你的异常信息!”);
自定义异常的格式 继承Exceptio类
- 1.自定义异常:
- class 异常类名 extends Exception
- {
- public 异常类名(String msg)
- {
- super(msg);
- }
- }
- 2.标识可能抛出的异常:
- throws 异常类名1,异常类名2
- 3.捕获异常:
- try{}
- catch(异常类名 y){}
- catch(异常类名 y){}
- 4.方法解释
- getMessage() //输出异常的信息
- printStackTrace() //输出导致异常更为详细的信息
习题源码如下:
1:MyException类
package com.lx.homework;
/**
*
* 封装异常信息类
*/
@SuppressWarnings("serial")
public class MyException extends Exception{
private String message;
public MyException(String message){
this.message=message;
}
public String getMessage(){
return message;
}
}
/**
*
* 封装异常信息类
*/
@SuppressWarnings("serial")
public class MyException extends Exception{
private String message;
public MyException(String message){
this.message=message;
}
public String getMessage(){
return message;
}
}
TextException类
package com.lx.homework;
import java.util.Scanner;
/**
*
* @author lixing
* 测试自定义异常类
*/
public class TestMyException {
@SuppressWarnings("static-access")
public static void main(String[] args) throws InstantiationException, IllegalAccessException {
System.out.println("--------请输入一个测试的数据:---------");
@SuppressWarnings("resource")
Scanner cin=new Scanner(System.in);
int n=cin.nextInt();
//1:反射获取Class的实例对象
try {
//通过动态加载类来获取Class的实例对象,或者是Student.class
@SuppressWarnings("rawtypes")
Class c1=Class.forName("com.lx.homework.Student");
//通过类类型来创建Student的对象.
Student stu=(Student)c1.newInstance();//可以不用反射的这里直接使用:Student stu=new Student();
try {
//反射方法调用
String Str=stu.speak(n);
System.out.println(Str);
} catch (MyException e){
e.printStackTrace();
//输出我定义的发生异常的信息
System.out.println(e.getMessage());
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
class Student{
public static String speak(int m) throws MyException{
if(m>1000){
throw new MyException("抱歉啊,您传入的数字大于1000了,请仔细检查!");
}
else{
return "恭喜呀,您输入的数字正确!";
}
}
}
import java.util.Scanner;
/**
*
* @author lixing
* 测试自定义异常类
*/
public class TestMyException {
@SuppressWarnings("static-access")
public static void main(String[] args) throws InstantiationException, IllegalAccessException {
System.out.println("--------请输入一个测试的数据:---------");
@SuppressWarnings("resource")
Scanner cin=new Scanner(System.in);
int n=cin.nextInt();
//1:反射获取Class的实例对象
try {
//通过动态加载类来获取Class的实例对象,或者是Student.class
@SuppressWarnings("rawtypes")
Class c1=Class.forName("com.lx.homework.Student");
//通过类类型来创建Student的对象.
Student stu=(Student)c1.newInstance();//可以不用反射的这里直接使用:Student stu=new Student();
try {
//反射方法调用
String Str=stu.speak(n);
System.out.println(Str);
} catch (MyException e){
e.printStackTrace();
//输出我定义的发生异常的信息
System.out.println(e.getMessage());
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
class Student{
public static String speak(int m) throws MyException{
if(m>1000){
throw new MyException("抱歉啊,您传入的数字大于1000了,请仔细检查!");
}
else{
return "恭喜呀,您输入的数字正确!";
}
}
}
运行结果:
--------请输入一个测试的数据:---------
1099
com.lx.homework.MyException: 抱歉啊,您传入的数字大于1000了,请仔细检查!
抱歉啊,您传入的数字大于1000了,请仔细检查!
1099
com.lx.homework.MyException: 抱歉啊,您传入的数字大于1000了,请仔细检查!
抱歉啊,您传入的数字大于1000了,请仔细检查!
at com.lx.homework.Student.speak(TestMyException.java:39)
at com.lx.homework.TestMyException.main(TestMyException.java:24)
7-3:
package com.lx.homework;
import java.util.Scanner;
/**
*
*@author lixing
* 测试自定义异常类
*/
public class TestComputer {
@SuppressWarnings("static-access")
public static void main(String[] args) throws InstantiationException, IllegalAccessException {
System.out.println("---------请输入两个数字:------");
@SuppressWarnings("resource")
Scanner cin=new Scanner(System.in);
int x=cin.nextInt();
int y=cin.nextInt();
@SuppressWarnings("rawtypes")
Class c1=Computer.class;
Computer Cop=(Computer)c1.newInstance();//可以不用反射,这里直接创建对象 Computer Cop=new Computer();
try {
Cop.CheckNumber(x, y);
} catch (MyException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
class Computer{
public static void CheckNumber(int m,int n) throws MyException{
if(m<0||n<0){
throw new MyException("您传入要计算最大公约数的数字不合法,请仔细检查!");
}
else{
while(m%n!=0){
int temp=m%n;
m=n;
n=temp;
}
}
System.err.println("计算的两个数的最大公约数为:"+n);
}
import java.util.Scanner;
/**
*
*@author lixing
* 测试自定义异常类
*/
public class TestComputer {
@SuppressWarnings("static-access")
public static void main(String[] args) throws InstantiationException, IllegalAccessException {
System.out.println("---------请输入两个数字:------");
@SuppressWarnings("resource")
Scanner cin=new Scanner(System.in);
int x=cin.nextInt();
int y=cin.nextInt();
@SuppressWarnings("rawtypes")
Class c1=Computer.class;
Computer Cop=(Computer)c1.newInstance();//可以不用反射,这里直接创建对象 Computer Cop=new Computer();
try {
Cop.CheckNumber(x, y);
} catch (MyException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
class Computer{
public static void CheckNumber(int m,int n) throws MyException{
if(m<0||n<0){
throw new MyException("您传入要计算最大公约数的数字不合法,请仔细检查!");
}
else{
while(m%n!=0){
int temp=m%n;
m=n;
n=temp;
}
}
System.err.println("计算的两个数的最大公约数为:"+n);
}
运行结果:
---------请输入两个数字:------
20 -4
您传入要计算最大公约数的数字不合法,请仔细检查!
com.lx.homework.MyException: 您传入要计算最大公约数的数字不合法,请仔细检查!
at com.lx.homework.Computer.CheckNumber(TestComputer.java:30)
at com.lx.homework.TestComputer.main(TestComputer.java:20)
20 -4
您传入要计算最大公约数的数字不合法,请仔细检查!
com.lx.homework.MyException: 您传入要计算最大公约数的数字不合法,请仔细检查!
at com.lx.homework.Computer.CheckNumber(TestComputer.java:30)
at com.lx.homework.TestComputer.main(TestComputer.java:20)
0 0
- Java 自定义异常解读
- JAVA异常解读
- Java异常解读
- java.lang.NoClassDefFoundError 异常解读
- java自定义注解并解读
- Java 异常(Exception)&自定义异常
- Java异常处理-----自定义异常
- Java 异常(Exception)&自定义异常
- JAVA-自定义异常/异常覆盖
- java异常体系、自定义异常
- Java异常之自定义异常
- Java异常之自定义异常
- Java 自定义异常 异常抛出
- Java异常处理--自定义异常
- Java异常之自定义异常
- 自定义异常实战,java封装自定义异常
- Java自定义异常
- java中自定义异常
- Glide – How Yelp’s Android App Loads Images
- TCP/IP
- centos7下修改主机名
- 基于日志理解 cms 原理,为什么remark要stop the world?(理解CMS GC日志.)
- Simditor修改缩进为首行缩进
- Java 自定义异常解读
- 数组常用的方法
- 4月第一篇
- 4月第二篇
- 4月第三篇
- 题目1498:寻找表达式
- 4月第四篇
- Binary Search Tree
- 【爬虫】爬取煎蛋上的段子