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. Spring源码学习(总)

    前文: ------------------------------------------------------------------------------------------------ ...

  2. mysql 服务器启用event_scheduler

    https://blog.csdn.net/yangzefei1991/article/details/51800867 首先在sql中查询计划事件的状态:SHOW VARIABLES LIKE 'e ...

  3. whmcs语言汉化路径

    前台语言访问文件夹/lang,后台语言文件放入/admin/lang

  4. shell入门(二)——面试题实例

    [~/shell]$ cat one.sh #!/bin/bash path=/root/shell/test.txt if [ ! -f $path ] //检测文件是否存在,如果不存在,把内容改为 ...

  5. day 57 jQuery的补充

    .data() 在匹配的元素集合中的所有元素上存储任意相关数据或返回匹配的元素集合中的第一个元素的给定名称的数据存储的值. .data(key, value): 描述:在匹配的元素上存储任意相关数据. ...

  6. 02基于python玩转人工智能最火框架之TensorFlow人工智能&深度学习介绍

    人工智能之父麦卡锡给出的定义 构建智能机器,特别是智能计算机程序的科学和工程. 人工智能是一种让计算机程序能够"智能地"思考的方式 思考的模式类似于人类. 什么是智能? 智能的英语 ...

  7. Atheros AR9285坑爹网卡仅仅有54M/65M,开启150M速率的方法

    版权声明:Max Sky 原创文章.转载时请保留全部权并以超链接形式标明文章出处.否则将追究相关法律责任. https://blog.csdn.net/maxsky/article/details/3 ...

  8. oracle-sql内置函数

    函数  oracle 自定义函数入门 主题:ORACLE函数大全 ############################### set operators UNION, UNION ALL, INT ...

  9. JCP与JSR

    JCP Java Community Process ,Java社区进程,Java标准指定组织JCP成立于1998年,官网,由社会各界Java组成的社区,规划和领导Java的发展,其成员可以在这里看到 ...

  10. java 实现websocket的三种方式

    Java中实现websocket常见有以下三种方式: 使用tomcat的websocket实现,需要tomcat 7.x,JEE7的支持. 使用spring的websocket,spring与webs ...