首页 > 编程语言 >java异常--自定义异常

java异常--自定义异常

时间:2022-09-27 16:23:38浏览次数:57  
标签:自定义 -- System Demo03 charpter6 异常 public

java异常--自定义异常

步骤:

  1. 创建自定义异常类。

  2. 在方法中通过throw关键字抛出异常对象。

  3. 处理异常try-catch 捕获并处理,否则在方法声明处通过throws关键字指明抛出给调用者的方法。

  4. 在异常方法的的调用者中捕获并处理异常。

package charpter6;


public class Demo03 extends Exception{
   //传递数字>10
   private int detail;
   public Demo03(int a){
       detail = a;
  }
   //toString   快捷键:Alt+insert
   //异常的打印信息
   @Override
   public String toString() {
       return "Exception{" + detail + '}';
  }

}
package charpter6.Demo02;

import charpter6.Demo03;

public class Test01 {
   //可能会存在异常的方法
   static void test(int a)throws Demo03{
       System.out.println("传递的参数为:"+a);
       if(a>10){
           throw new Demo03(a); //抛出
      }
       System.out.println("ok");
  }

   public static void main(String[] args) {
       try{
           test(11);
      }catch(Demo03 e){
           System.out.println("Exception:"+e);
      }
  }
}
 

 

标签:自定义,--,System,Demo03,charpter6,异常,public
From: https://www.cnblogs.com/ssl-study/p/16734942.html

相关文章

  • ansible 修改/etc/hosts
    1、编辑文件hosts.tmlvim hosts.tml【----hosts: 192.168.59.103 remote_user:root tasks:  -name:addhosts   lineinfile:name=/etc/hostslin......
  • vmware虚拟机安装教程win10详细步骤
    vmware虚拟机安装教程win10详细步骤自从win10发布以后很多人都去升级系统体验了一下,有的用户想体验却不想重装win10系统,这时我们只能靠vmware虚拟机来安装win10系统进行......
  • FCKEditorword导入编辑
    ​ 当前功能基于PHP,其它语言流程大抵相同。大概流程:1.将docx文件上传到服务器中2.使用PHPoffice/PHPword实现将word转换为HTML3.将HTML代码返回并赋值到编辑器中......
  • The engine "node" is incompatible with this module.
    root@test-OptiPlex-3050:/home/web/hflab#yarninstallyarninstallv1.22.19warningpackage-lock.jsonfound.Yourprojectcontainslockfilesgeneratedbytools......
  • odoo 上传的文件怎样保存
    路由@http.route('/web/binary/upload_attachment',type='http',auth="user")@serialize_exceptiondefupload_attachment(self,callback,model,id,......
  • 【Idea】idea 配置自动下载依赖
    设置-构建-maven-导入-勾选动下载   pom文件右键-maven-重新加载 ......
  • 01.web基础
    概述前端开发:web开发,通过html,css,javascript等以及它们衍生出来的技术和框架,来实现互联网产品的用户界面交互设计网页、网站:网页是单独的静态的页面,不需要后端程序,网......
  • 【安全测试】【sqlmap】sqlmap快速入门
    一、目的本文主要介绍如何用sqlmap来测试某个接口是否有sql注入的风险,以及查看对应的注入的payload。sqlmap支持-u指定接口的url信息,也支持-r来解析文件中的请求信息。......
  • 9.27比赛记录
    T1不会,再见。/fnT2题意有\(n\)个奶牛排成一列,每次队头的牛都会插到第倒数\(c_i\)个位置上,问有多少个牛无法到达第一位。思路是道很厉害的二分。可惜赛时没打出来......
  • SpringBoot+Vue学生选课系统 学生课程信息系统 学生课程表管理系统Java
    ......