java抛出异常 java异常处理机制示例(java抛出异常、捕获、断言)(IT技术)

您所在的位置:网站首页 initcause方法 java抛出异常 java异常处理机制示例(java抛出异常、捕获、断言)(IT技术)

java抛出异常 java异常处理机制示例(java抛出异常、捕获、断言)(IT技术)

#java抛出异常 java异常处理机制示例(java抛出异常、捕获、断言)(IT技术)| 来源: 网络整理| 查看: 265

package com.hongyuan.test;

import java.io.IOException;import java.util.logging.Level;import java.util.logging.Logger;

public class ExceptionHandleTest {

 static{  //开启断言,此后由系统类加载器加载的类将启用断言。  ClassLoader.getSystemClassLoader().setDefaultAssertionStatus(true); }

 public static void main(String[] args) {  /*   * 抛出,捕获   */  try {   TryCatchTest.run(10, -1);  } catch (IOException e) {   e.printStackTrace();  }  System.out.println("====================================================");

  //日志  LogerTest.run();

  System.out.println("====================================================");  //断言  AssertTest.div(3,0);

 }

}

/* * 断言 */class AssertTest {

 public static double div(int b,int a){

  assert a!=0:"你这么用,你小学老师知道吗?";

  return (double)b/a; }}

/* * 日志 */class LogerTest {

 private static Logger logger=null;

 static{  //获取日志对象并定义日志级别  logger=Logger.getLogger(LogerTest.class.getName());  logger.setLevel(Level.ALL); }

 public static void run(){  //进入方法  logger.entering(LogerTest.class.getName(), "run");  //普通信息  logger.info("又来找我麻烦,这笔账我记下了!!!");  //警告  logger.warning("太累了,这活没法干了!!!");  //严重  logger.log(Level.SEVERE,"老子不干了!!! ^O^");  //退出方法  logger.exiting(LogerTest.class.getName(), "run"); }}

/* * 捕获,抛出 */class TryCatchTest {

 public static void run(int x,int y) throws IOException {

  try{//必须

   if(x



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3