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. Blender界面及模式统计

    Blender2.79b安装后默认界面分布: 所有窗口: 窗口: 3D View的9种模式:

  2. LeetCode - Beautiful Array

    For some fixed N, an array A is beautiful if it is a permutation of the integers 1, 2, ..., N, such ...

  3. POJ3904 Sky Code

    题意 Language:Default Sky Code Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3980 Accepte ...

  4. Dynamics CRM Solution

    Default solution Dynamics comes pre-loaded with a Default Solution Contains all the base objects, en ...

  5. 我发起了一个 .Net 开源 数据库 项目 SqlNet

    大家好 , 我发起了一个 .Net 开源 数据库 项目 SqlNet . 项目计划 是 用 C# 写一个 关系数据库 . 可以先参考我之前写的 2 篇文章 : 谈谈数据库原理    https://w ...

  6. 打造mac上最好用的Terminal

    出处:https://blog.csdn.net/kebing1011/article/details/46934533?utm_source=blogxgwz0

  7. Python问题汇总

    1.dict is not callable tree是一个字典类型. tree("left") -> tree["left"]   2.list ind ...

  8. PyCharm 连接Git及使用

    一.PyCharm配置Git的环境 1.PyCharm 连接Git首先需要本机安装Git软件; 2.PyCharm 版本控制中设置Git的执行路径,file->Setting->Versi ...

  9. vs2013 使用vs2017的localdb

    应用vs203进行MVC开发时,进行数据库初始化的时候,默认使用电脑中高版本的localdb(v12),在修改web.config中的链接串时报错,也无法使能数据库迁移, 解决方法:在数据库初始化之前 ...

  10. 阅读OReilly.Web.Scraping.with.Python.2015.6笔记---BeautifulSoup---findAll

    阅读OReilly.Web.Scraping.with.Python.2015.6笔记---BeautifulSoup---findAll 1..BeautifulSoup库的使用 Beautiful ...