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. eclipse + cdt

    Window > Preferences > General > Appearance中设置主题颜色. Help > eclipse marketplace > find ...

  2. GinKgoCTF-Misc

    一:谁动了我的校徽? Jpg改txt——>寻找——>GKCTF{This_is_a_huaji} 二:奇怪的压缩包1 六位数字的密码一点也不安全!!!!!! 下载压缩包——>有密码( ...

  3. java transient 知识点学习

    今天看源码的时候看到这个关键字,完全没见过,不懂.好吧!学习一下. 我们都知道实现了Serilizable接口的类,便可以序列化,那么其中某些成员变量不想被序列化怎么办?就是利用transient这个 ...

  4. maven 总结整理(二)——download source code

    当我们用maven下载jar包时,有时希望下载jar包的源代码,此时可以在pom.xml文件中,进行设置. <build>    <finalName>WebProject&l ...

  5. day28Spark

    PS:因为Spark是用内存运行 的,非常快 PS: 1.下面就是将conf的spark-env.template改变成spark-env.sh,并添加红色部分 2.修改slaves文件添加从设备 启 ...

  6. day3 反射与动态代理

    import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Metho ...

  7. 利用 httpmodule 强制所有页面使用同一基类

    public class OMSPageChecker : IHttpModule { public void Dispose() { } public void Init(HttpApplicati ...

  8. mysql export query result

    1 . export by shell a.sql use dbname; SELECT id,iab_num FROM iab_list ; mysql -h host -uusername -P3 ...

  9. 使用ipns 为ipfs 系统自定义域名

    ipns 可以帮助我们进行寻址操作,但是默认的hashid 还是太长,不好记忆,ipns 同时也支持 基于域名的解析,我们添加txt 记录就可以方便的解决ipfs 文件访问地址难记的问题,使用的是 一 ...

  10. 主机-配件-接口-整机-3c-2

    pc机,服务器,智能手机与各种嵌入式乃至物联网 http://www.mifalife.net/hk/mall.html MIFA F5 户外无线蓝牙音箱2.0声道高保真可通话插卡便携低音炮迷你iph ...