理解异常的基本概念;
掌握异常处理方法及熟悉常见异常的捕获方法。
练习捕获异常、声明异常、抛出异常的方法、熟悉try和catch子句的使用。
掌握自定义异常类的方法。
编写一个类,在其main()方法中创建一个一维数组,在try字句中访问数组元素,使其产生ArrayIndexOutOfBoundsException异常。在catch子句里捕获此异常对象,并且打印“数组越界”信息,加一个finally子句,打印一条信息以证明这里确实得到了执行。
自定义异常类的使用
车站检查危险品的设备,如果发现危险品会发出警告。编程模拟设备发现危险品。
代码
package hello.java;
public class Yichan {
public static void main(String[] args) {
  int a[]=new int [3];
  
  try
  {
      for(int i=0;i<4;i++)
      
         System.out.println(a[i]); 
}catch(ArrayIndexOutOfBoundsException A)
  {
     System.out.println("数组越界"); 
  }
  
 finally
 {
     System.out.println("程序正常执行");
 }
}
}
编写一个Exgeption的子类DangerException,该子类可以创建异常对象,该异常对象调用toShow()方法输出“危险物品”。编写一个Machine类,该类的方法checkBag(Goods goods)当发现参数goods是危险品时(goods的isDanger属性是true)将抛出DangerException异常。
程序在主类的main()方法中的try-catch语句的try部分让Machine类的实例调用checkBag(Goods goods)的方法,如果发现危险品就在try-catch语句的catch部分处理危险品。
代码
package hello.java;
@SuppressWarnings("serial")
public class DangerException  extends Exception  {
public void toShow() {
    
    System.err.println("危险物品");
}
}
 package hello.java;
public class Goods {
  private   String name;
  boolean isDanger;
  public boolean isDanger() {
        
        return isDanger;
    }
public   String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public void setDanger(boolean isDanger) {
    this.setIsDanger(isDanger);
}
public Object getIsDanger() {
    return isDanger;
}
public void setIsDanger(Object isDanger) {
    this.isDanger = (boolean) isDanger;
}
    }
package hello.java;
public class JIqi {
public void checkBag (Goods goods) throws DangerException
 {
if(goods.isDanger()){  
    
 DangerException isdanger=new DangerException(); 
throw isdanger;
}
}
}
package hello.java;
public class Check {
public static void main(String[] args) {
    
    JIqi qi = new JIqi();
    Goods goods = new Goods();
    String  name []={"可乐","雪碧","菠萝啤","毒酒"};
     for(int i=0;i<name.length;i++)
     {
         goods.setName(name[i]);
     if(name[i].equals("毒酒")) {
         goods.isDanger();
         System.err.println(name[i]+"是危险品");
     }
     else 
         System.out.println(name[i]+"是安全品");
     }
     try {
         qi.checkBag(goods);
     }catch(DangerException d) {
     d.toShow();
     }
 }
}心得:这题就是让我们应用throw抛出异常,不让程序中断,让程序捕捉到危险品,再输出,所谓异常的生活应用,这题是有点难,好在上课拍了几张图片,还稳得住

  try 
   {   可能异常语句
      }catch(异常类 异常对象)
  {
       编写异常处理语句
   }finally{
          一定会运行到的程序代码
    }  class Math{
 public int div(int i,int j)throws Exception{
 int temp=i/j;
 return temp;
}
}  public class ThrowDemo01{
  public  static  void  main (string args[]){
  try{
      throw new Exception ("自己抛出异常!")                            
 }catch(Exception e)  {
  System.out.println(e);
  }
   }
   }class  myException  extends Exception{
             public   MyException(String msg){
              super(msg)
}
}
public class DefaultException
 public static void main(String[] args) {
 try{
     throw new  MyException("自定义异常。");
      }catch(Exception e);
{
System.out.println(e);
 }
}
}原文:https://www.cnblogs.com/1793979463hyx/p/11694369.html