import java.util.Scanner;

public class MyExceptionTest {

    public static void check(Square A) throws WrongException
{
if(A.getChang()<=0 && A.getKuan()<=0){
throw new WrongException("长<=0,不合法, 宽<=0,也不合法");
}
if(A.getKuan()<=0){
throw new WrongException("宽<=0,不合法");
}
if(A.getChang()<=0){
throw new WrongException("长<=0,不合法");
}
A.CalcuArea();
} public static void main(String[] args) {
// TODO Auto-generated method stub
Square A=new Square();
Scanner in =new Scanner(System.in);
System.out.println("请输入矩形参数:\n"+"长: 宽:\n");
int Chang=in.nextInt();
A.setChang(Chang);
int kuan=in.nextInt();
A.setKuan(kuan); try{
check(A);
System.out.println(A.toString());
}catch(WrongException e){
System.out.println("报错啦\n"+e.getMessage());
e.toString();
e.printStackTrace();
}finally{
System.out.println("谢谢使用,再见!");
}
}
} class Square
{
private int Chang,Kuan;
private int Area; public int getChang() {
return Chang;
}
public String toString() {
return "矩形 [长=" + Chang + ", 宽=" + Kuan + ", 面积=" + Area + "]";
} public void setChang(int chang) {
Chang = chang;
} public int getKuan() {
return Kuan;
} public void setKuan(int kuan) {
Kuan = kuan;
}
public void CalcuArea()
{
Area=Chang*Kuan;
}
} class WrongException extends Exception { //定义一个Message
String Message; //构造方法,设置Message
public WrongException(String Message) {
this.Message = Message;
} //输出Message
public String getMessage()
{
return Message;
}
}

public class Test1 {

    public static void main(String[] args) {
// TODO Auto-generated method stub
int a = 100, b = 0, c;
try{
c = a / b;
System.out.println("c=" + c);
}catch(ArithmeticException e){
System.out.println("catched!");
System.out.println("catch ArithmeticException message:"+
e.getMessage());
System.out.println("catch ArithmeticException toSting():"
+ e.toString());
e.printStackTrace();//打印出现错误的地方
}finally{
System.out.println("finally!");
}
} }

public class Test4 {

    public static void main(String[] args) {
int a = 100, b = 2, c = 0;
int[] x = { 10, 20, 30, 40, 50, 60, 70 };
// 如果try写在for循环外面,异常后就跳出,将不再进入循环
for (int i = 0; i <= 10; i++) {
System.out.println("c=" + c);
try {
c = a / b--;
System.out.println("x[" + i + "]=" + x[i]);
} catch (ArithmeticException e) {
System.out.println("catched ArithmeticException message:" + e.getMessage());
System.out.println("catched ArithmeticException toSting():" + e.toString());
e.printStackTrace();
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("catched ArrayIndexOutOfBoundsException!");
} finally {
System.out.println("finally!");
}
}
} }
c=0
x[0]=10
finally!
c=50
x[1]=20
finally!
c=100
catched ArithmeticException message:/ by zero
catched ArithmeticException toSting():java.lang.ArithmeticException: / by zero
java.lang.ArithmeticException: / by zero
at Test4.main(Test4.java:11)
finally!
c=100
x[3]=40
finally!
c=-100
x[4]=50
finally!
c=-50
x[5]=60
finally!
c=-33
x[6]=70
finally!
c=-25
catched ArrayIndexOutOfBoundsException!
finally!
c=-20
catched ArrayIndexOutOfBoundsException!
finally!
c=-16
catched ArrayIndexOutOfBoundsException!
finally!
c=-14
catched ArrayIndexOutOfBoundsException!
finally!
//使用 throw 语句抛出异常、使用 throws 子句抛弃异常
public class TestThrow1 {
static void throwProcess(Object o) throws NullPointerException{// throws NullPointerException可不写
if(o==null){
throw new NullPointerException("空指针异常");//手动写抛出异常
}
//若抛出异常则不再执行函数下面的语句
System.out.println(o+"哈哈呵呵");
}
public static void main(String[] args) {
Object p=null;
try{
throwProcess(p);
}catch(NullPointerException e){
System.out.println("捕获:" + e);
}
} }

Java学习---异常处理的更多相关文章

  1. Java学习---异常处理的学习

    基础知识 任何一门计算机程序设计语言都包括有绝对正确和相对正确的语句.绝对正确: 指任何情况下, 程序都会按照流程正确执行:相对正确: 程序的运行受到运行环境的制约, 在这种情况下, 需要附加检测和控 ...

  2. Java学习--异常处理及其应用类

    异常是运行时在代码序列中引起的非正常状况,换句话说,异常是运行时错误.在不支持异常处理的计算机语言中,必须手动检查和处理错误----通常是通过使用错误代码,等等.这种方式既笨拙又麻烦.Java的异常处 ...

  3. java学习——异常处理

     类  Throwable类       Java 语言中所有错误或异常的超类.只有当对象是此类(或其子类之一)的实例时,才能通过 Java 虚拟机或者 Java throw语句抛出.类似地,只有此类 ...

  4. java学习——异常处理机制

    public class ExceptionDemo2 { public static void main(String[] args) { // TODO Auto-generated method ...

  5. Java 学习(10):java 异常处理

    java 异常处理 异常发生的原因有很多,通常包含以下几大类: 用户输入了非法数据. 要打开的文件不存在. 网络通信时连接中断,或者JVM内存溢出. 三种类型的异常: 检查性异常: 最具代表的检查性异 ...

  6. JAVA学习之 异常处理机制

    今天就来说说java的异常处理机制,异常处理不是第一接触,尤其是写过非常多c#的代码,基本都会写到异常处理的代码,事实上c#的异常处理与java的异常处理基本都是一样的,仅仅是在一些细节上不是非常一样 ...

  7. Java 学习笔记(11)——异常处理

    异常是程序中的一些错误,但并不是所有的错误都是异常,并且错误有时候是可以避免的. 比如说,你的代码少了一个分号,那么运行出来结果是提示是错误 java.lang.Error:如果你用System.ou ...

  8. 《Java学习笔记(第8版)》学习指导

    <Java学习笔记(第8版)>学习指导 目录 图书简况 学习指导 第一章 Java平台概论 第二章 从JDK到IDE 第三章 基础语法 第四章 认识对象 第五章 对象封装 第六章 继承与多 ...

  9. 关于JAVA学习计划和感想

    学习计划第一阶段:    JAVA语言基础知识.包括异常.IO流.多线程.集合类.    要求:异常------掌握try-catch-finally的使用          IO流------掌握字 ...

随机推荐

  1. MyBatis sql语句使用总结

    MyBatis中Like语句使用总结 oracle数据库: SELECT * FROM user WHERE name like CONCAT('%',#{name},'%') 或 : SELECT ...

  2. 安装,配置,启动FTP,SSH,NFS服务

    1.安装,配置,启动FTP服务 sudo apt-get install vsftpd 修改vsftpd的配置文件/etx/vsftpd/.config,将下面几行前面的“#”去掉 #local_en ...

  3. struts2文件上传1

    <form action="hello/UploadAction_upload.action" enctype="multipart/form-data" ...

  4. Go Example--切片

    package main import ( "fmt" ) func main() { //make来初始化一个切片,必须指名切片的长度 s:= make([]string, 3) ...

  5. 洛谷P1337 【[JSOI2004]平衡点 / 吊打XXX】(模拟退火)

    洛谷题目传送门 很可惜,充满Mo力的Mo拟退火并不是正解.不过这是一道最适合开始入手Mo拟退火的好题. 对模拟退火还不是很清楚的可以看一下 这道题还真和能量有点关系.达到平衡稳态的时候,物体的总能量应 ...

  6. zabbix入门之使用QQ邮箱接受报警信息

    首先说明我使用的是3.2版本的zabbix 既然要发邮件到QQ邮箱报警,那么在centos7上就肯定需要安装发送邮件的软件了 安装应用yum install mailx sendmail -y接着修改 ...

  7. ES5新增数组方法

    forEach/map   every/some  indexOf/lastIndexOf  filter reduce Array.isArray

  8. Add task bar to ubuntu

    http://www.howtogeek.com/189819/how-to-add-a-taskbar-to-the-desktop-in-ubuntu-14.04/ sudo apt-get in ...

  9. 关于value_count

    value_counts将会对于指定列的数据进行group,然后统计出各个出现的值的数量,并且按照从高到低的顺序进行排序 train_data = load_titanic_data("tr ...

  10. 商品和订单中使用MQ

    一.将Product服务增加到配置中心 1.添加引用 <dependency> <groupId>org.springframework.cloud</groupId&g ...