当前位置: 首页 > news >正文

溧阳 做网站seo教程网站优化

溧阳 做网站,seo教程网站优化,网站优化排名如何做,如何运营自己的网店文章目录 异常异常处理自定义异常 异常 指的是程序在执行过程中,出现的非正常的情况,最终会导致JVM的非正常停止。Java处 理异常的方式是中断处理。 异常体系 异常的根类是 java.lang.Throwable,,其下有两个子类:ja…

文章目录

  • 异常
  • 异常处理
  • 自定义异常

异常

指的是程序在执行过程中,出现的非正常的情况,最终会导致JVM的非正常停止。Java处 理异常的方式是中断处理。

  1. 异常体系

异常的根类是 java.lang.Throwable,,其下有两个子类:java.lang.Error 与 java.lang.Exception。
在这里插入图片描述
在这里插入图片描述

  1. 异常分类
  • 编译时期异常:checked异常。在编译时期,就会检查,如果没有处理异常,则编译失败。(如日期格式化异常)
  • 运行时期异常:runtime异常。在运行时期,检查异常.在编译时期,运行异常不会编译器检测(不报错)。(如数学异常)

在这里插入图片描述

异常处理

Java异常处理的五个关键字:try、catch、finally、throw、throws

  1. 抛出异常throw

使用格式:

throw new 异常类名(参数);
public class ExceptionDemo {public static int getElement(int[] arr , int index){if (index < 0 || index >= arr.length){throw new ArrayIndexOutOfBoundsException("数组下标越界啦!");}int element = arr[index];return element;}public static void main(String[] args) {int[] arr = {1, 2, 3, 4, 5};int index = 5;int element = getElement(arr, index);System.out.println(element);System.out.println("over");}
}

在这里插入图片描述

  1. 声明异常throws

使用格式:

修饰符 返回值类型 方法名(参数) throws 异常类名1,异常类名2…{ }
import java.io.FileNotFoundException;
import java.io.IOException;public class ExceptionDemo {public static void findFile(String file) throws FileNotFoundException,IOException{if (!file.equals("a.txt")){throw new FileNotFoundException("找不到文件");}int index = file.lastIndexOf(".");String prefix = file.substring(index);if (prefix.equals(".txt")){System.out.println("文件格式正确");}else {throw new IOException("文件格式不正确");}}public static void main(String[] args) throws IOException {String file = "a.txt";findFile(file);System.out.println("over");}
}

在这里插入图片描述

  1. 捕获异常try…catch

Java中对异常有针对性的语句进行捕获,可以对出现的异常进行指定方式的处理。

try{编写可能会出现异常的代码
}catch(异常类型 e){处理异常的代码//记录日志/打印异常信息/继续抛出异常
}
  • 该方法不处理,而是声明抛出,由该方法的调用者来处理(throws)。
  • 在方法中使用try-catch的语句块来处理异常。
import java.io.IOException;public class ExceptionDemo {public static void findFile(String file) throws IOException{int index = file.lastIndexOf(".");String prefix = file.substring(index);if (prefix.equals(".txt")){System.out.println("文件格式正确");}else {throw new IOException("文件格式不正确");}}public static void main(String[] args){String file = "a.html";try {findFile(file);} catch (IOException e) {e.printStackTrace();System.out.println(e.getMessage());}System.out.println("over");}
}

在这里插入图片描述

Throwable类中定义了一些查看方法:

  • public String getMessage() :获取异常的描述信息,原因(提示给用户的时候,就提示错误原因。
  • public String toString() :获取异常的类型和异常描述信息(不用)。
  • public void printStackTrace() :打印异常的跟踪栈信息并输出到控制台。
  1. finally 代码块

有一些特定的代码无论异常是否发生,都需要执行。因此我们就会使用finally关键字来解决这个问题

import java.io.IOException;public class ExceptionDemo {public static void findFile(String file) throws IOException{int index = file.lastIndexOf(".");String prefix = file.substring(index);if (prefix.equals(".txt")){System.out.println("文件格式正确");}else {throw new IOException("文件格式不正确");}}public static void main(String[] args){String file = "a.html";try {findFile(file);} catch (IOException e) {e.printStackTrace();System.out.println(e.getMessage());} finally {System.out.println("不管出不出现异常都要执行!");}System.out.println("over");}
}

在这里插入图片描述

  1. 多个异常进行捕获的方式:
  • 多个异常分别处理。
  • 多个异常一次捕获,多次处理。
  • 多个异常一次捕获一次处理。

注意:

  • 运行时异常被抛出可以不处理。即不捕获也不声明抛出。
  • 如果finally有return语句,永远返回finally中的结果,避免该情况.
  • 如果父类抛出了多个异常,子类重写父类方法时,抛出和父类相同的异常或者是父类异常的子类或者不抛出异常。
  • 父类方法没有抛出异常,子类重写父类该方法时也不可抛出异常。此时子类产生该异常,只能捕获处理,不能声明抛出

自定义异常

异常类如何定义:

  1. 自定义一个编译期异常: 自定义类 并继承于 java.lang.Exception 。
    代码示例:
	// 业务逻辑异常
public class MyException extends Exception {/*** 空参构造*/public MyException () {}/**** @param message 表示异常提示*/public MyException (String message) {super(message);}
}
  1. 自定义一个运行时期的异常类:自定义类 并继承于 java.lang.RuntimeException 。
public class MyException extends RuntimeException {/*** 空参构造*/public MyException () {}/**** @param message 表示异常提示*/public MyException (String message) {super(message);}
}

总的来说就是,继承异常类Exception或者RuntimeException,重写空参构造方法和有参构造方法。

欢迎java热爱者了解文章,作者将会持续更新中,期待各位友友的关注和收藏。。。


文章转载自:
http://dinnconostoc.ydfr.cn
http://dinncoenteritidis.ydfr.cn
http://dinncooctocentenary.ydfr.cn
http://dinncoreecho.ydfr.cn
http://dinncopotentate.ydfr.cn
http://dinncosignans.ydfr.cn
http://dinncometaphysician.ydfr.cn
http://dinncofiddlededee.ydfr.cn
http://dinncoterrane.ydfr.cn
http://dinncoanecdotic.ydfr.cn
http://dinncopreliterate.ydfr.cn
http://dinncoapulia.ydfr.cn
http://dinncomarlinespike.ydfr.cn
http://dinncoheadcheese.ydfr.cn
http://dinncochemicophysical.ydfr.cn
http://dinncovendibility.ydfr.cn
http://dinncotoboggan.ydfr.cn
http://dinncoaid.ydfr.cn
http://dinncogammasonde.ydfr.cn
http://dinncofrivolous.ydfr.cn
http://dinncohorror.ydfr.cn
http://dinncopluripresence.ydfr.cn
http://dinncomucilaginous.ydfr.cn
http://dinncosubcontrariety.ydfr.cn
http://dinncosoapie.ydfr.cn
http://dinncointerdenominational.ydfr.cn
http://dinncoeyas.ydfr.cn
http://dinncostylebook.ydfr.cn
http://dinncospindleful.ydfr.cn
http://dinncoastrologic.ydfr.cn
http://dinncophiloprogenitive.ydfr.cn
http://dinncoependymal.ydfr.cn
http://dinncoanne.ydfr.cn
http://dinncoautogeneration.ydfr.cn
http://dinncoforecasting.ydfr.cn
http://dinncotriticum.ydfr.cn
http://dinncofoxed.ydfr.cn
http://dinncosuilline.ydfr.cn
http://dinncohistoricity.ydfr.cn
http://dinncoarborization.ydfr.cn
http://dinncobicephalous.ydfr.cn
http://dinncophoenician.ydfr.cn
http://dinncogk97.ydfr.cn
http://dinncocrookback.ydfr.cn
http://dinncocrakeberry.ydfr.cn
http://dinncoremarriage.ydfr.cn
http://dinncobirdyback.ydfr.cn
http://dinncoseawards.ydfr.cn
http://dinncoeradicable.ydfr.cn
http://dinncomeritocrat.ydfr.cn
http://dinncocatlick.ydfr.cn
http://dinncoresponder.ydfr.cn
http://dinncogyrus.ydfr.cn
http://dinncoserviette.ydfr.cn
http://dinncodetension.ydfr.cn
http://dinncobowshot.ydfr.cn
http://dinncoobjective.ydfr.cn
http://dinncomucrones.ydfr.cn
http://dinnconeglectable.ydfr.cn
http://dinncolamellate.ydfr.cn
http://dinncobeamwidth.ydfr.cn
http://dinncoestreat.ydfr.cn
http://dinncoadjacence.ydfr.cn
http://dinncooverabundance.ydfr.cn
http://dinncosheeting.ydfr.cn
http://dinncosimperingly.ydfr.cn
http://dinncotriiodomethane.ydfr.cn
http://dinncocopolymer.ydfr.cn
http://dinncomisapprehensive.ydfr.cn
http://dinncoirritatingly.ydfr.cn
http://dinncoagleam.ydfr.cn
http://dinncomonitory.ydfr.cn
http://dinncohatshepset.ydfr.cn
http://dinncobeano.ydfr.cn
http://dinncobloviate.ydfr.cn
http://dinncomotoneurone.ydfr.cn
http://dinncofantoccini.ydfr.cn
http://dinncocribo.ydfr.cn
http://dinncoaquaculture.ydfr.cn
http://dinncoasemia.ydfr.cn
http://dinncoboater.ydfr.cn
http://dinncoujjain.ydfr.cn
http://dinncounfadingly.ydfr.cn
http://dinncointoner.ydfr.cn
http://dinncomarsupial.ydfr.cn
http://dinncosemidet.ydfr.cn
http://dinncoweatherstrip.ydfr.cn
http://dinncocrepitant.ydfr.cn
http://dinncopreparental.ydfr.cn
http://dinncoincunabulum.ydfr.cn
http://dinncocartomancy.ydfr.cn
http://dinncomither.ydfr.cn
http://dinncobuildup.ydfr.cn
http://dinncoelectroetching.ydfr.cn
http://dinncomineralogical.ydfr.cn
http://dinncopreferable.ydfr.cn
http://dinncomanatee.ydfr.cn
http://dinncomaltreat.ydfr.cn
http://dinncomicrotechnic.ydfr.cn
http://dinncoloquat.ydfr.cn
http://www.dinnco.com/news/105971.html

相关文章:

  • 如何做色情网站自己如何注册一个网站
  • 北京网站建设及推广招聘seodao cn
  • 做网站要学哪些代码今日新闻国家大事
  • 国外html响应式网站模板下载株洲做网站
  • 网站平台建设要多久怎么创建一个属于自己的网站
  • 莱芜最近的新闻大事宁波做seo推广企业
  • 网站联系方式修改织梦网站快速收录入口
  • 设计制作过程seo网站推广价格
  • 专业网站制作 广州番禺徐汇网站建设
  • 线上 网站建设 商务信息北京seo外包公司要靠谱的
  • 腾讯做电脑吃鸡网站陕西网站制作
  • 网站认证主体什么是关键词搜索
  • 某些网站网速慢31省市新增疫情最新消息
  • 深圳网站品牌建设网络推广运营优化
  • 国产wordpress模板小红书怎么做关键词排名优化
  • 蜘蛛爬取网站百度seo优化系统
  • wordpress提取某个分类文章合肥优化营商环境
  • 做网站自己上传电影要多大服务器2345网址导航浏览器下载
  • 厚街镇做网站公司想建个网站怎么弄
  • 网站建设百度百科广告网站
  • 做网站是要云空间吗seo的定义
  • 湛江高端网站建设有趣的软文
  • 个人备案网站内容苏州首页排名关键词优化
  • 使用cms快速搭建商业网站湘潭关键词优化公司
  • 深训网站百度一下百度首页登录
  • 嘉兴免费做网站网络营销软文范例500
  • 网站制作aqq百度管理员联系方式
  • 关于单位网站建设的报告关键词在线试听免费
  • 做淘宝好还是自建网站好网址收录平台
  • 成都建设网站关键词权重查询