常见的运行时异常

news/2024/7/4 0:02:58
//******************以下是运行时异常***************************
	//ArithmeticException
	@Test
	public void test6(){
		int a = 10;
		int b = 0;
		System.out.println(a / b);
	}
	
	//InputMismatchException
	@Test
	public void test5(){
		Scanner scanner = new Scanner(System.in);
		int score = scanner.nextInt();
		System.out.println(score);
		
		scanner.close();
	}
	
	//NumberFormatException
	@Test
	public void test4(){
		
		String str = "123";
		str = "abc";
		int num = Integer.parseInt(str);
		
		
		
	}
	
	//ClassCastException
	@Test
	public void test3(){
		Object obj = new Date();
		String str = (String)obj;
	}
	
	//IndexOutOfBoundsException
	@Test
	public void test2(){
		//ArrayIndexOutOfBoundsException
//		int[] arr = new int[10];
//		System.out.println(arr[10]);
		//StringIndexOutOfBoundsException
		String str = "abc";
		System.out.println(str.charAt(3));
	}
	
	//NullPointerException
	@Test
	public void test1(){
		
//		int[] arr = null;
//		System.out.println(arr[3]);
		
		String str = "abc";
		str = null;
		System.out.println(str.charAt(0));
		
	}

http://www.niftyadmin.cn/n/1791017.html

相关文章

NoSuchMethodError: ... addOnCompleteCallback

问题描述: 使用ES 2.3.1和Spark streaming 2.1时,出现以上报错信息。 原因: addOnCompleteCallback方法在spark2.0中移除了 The addOnCompleteCallback method was removed from the TaskContext in Spark 2.0. Spark 2.0 is not binary comp…

PHP实现excel导出

2019独角兽企业重金招聘Python工程师标准>>> 1&#xff1a;前端代码 <div id‘export’>导出excel表单</div> // //导入excel文件 $("#export").on(click, function(){ $.ajax({ url:"importexcel.php&quo…

异常处理机制:两种方式

throws 异常类型 import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException;/*** author xianyu* version 1.0* date 2020/1/1 15:53*/ public class ExceptionTest2 {public static void main(String[] arg…

MapReduce之Reduce Join

一 介绍 Reduce Join其主要思想如下&#xff1a; 在map阶段&#xff0c;map函数同时读取两个文件File1和File2&#xff0c;为了区分两种来源的key/value数据对&#xff0c;对每条数据打一个标签&#xff08;tag&#xff09;&#xff0c; 比如&#xff1a;tag0表示来自文件File1…

Java的23种设计模式之单列设计模式

饿汉式单列模式类&#xff1a;在这个类每次加载到内存中就直接把instance实例化好了 package com.xianyu.singleton;/*** author xianyu* version 1.0* date 2020/3/12 16:35* 设计模式1: 单列设计模式*/ public class Program1 {public static void main(String[] args) {// …

vue-cli keep-alive用法以及activated,deactivated

keep-alive用法 <keep-alive>是Vue的内置组件&#xff0c;能在组件切换过程中将状态保留在内存中&#xff0c;防止重复渲染DOM。 include: 字符串或正则表达式。只有匹配的组件会被缓存。exclude: 字符串或正则表达式。任何匹配的组件都不会被缓存。import Vue from vue …

小型电子声光礼花器电子烟花爆竹电路设计

节日和庆典时燃放礼花&#xff0c;其绚丽缤纷的图案&#xff0c;热烈的爆炸声、欢乐的气氛&#xff0c;能给人们留下美好的印象&#xff0c;但有一定的烟尘污染和爆炸危险隐患。本电路可以模拟礼花燃放装置&#xff0c;达到声型兼备的效果&#xff0c;给人们在安全、环保的环境…

spring boot 配置 JPA

application.properties的配置 spring.datasource.platformpostgres spring.datasource.urljdbc:postgresql://localhost:5432/to_database spring.datasource.usernamepostgres spring.datasource.password123456789 spring.jpa.properties.hibernate.dialect org.hibernate.…