1、示例一
// try-catch处理异常
public class Error {
public static void main(String[] args) {
int num1=34,num2=0;
//使用try包裹住会产生异常的代码段
try{
System.out.println(num1/num2);
}
//使用catch去处理对应的异常
catch(ArithmeticException error){
//处理方法
System.err.println("运算错误,除数不能为0!");
}
System.out.println("程序运行结束!");
}
}结果验证:
运算错误,除数不能为0!
程序运行结束!
2、示例二
import java.util.InputMismatchException;
import java.util.Scanner;
public class Error {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入第一个数字:");
//使用try包裹住会产生异常的代码段
try{
int num1=input.nextInt();
System.out.println("请输入第二个数字:");
int num2=input.nextInt();
System.out.println(num1/num2);
}
//使用catch去处理对应的异常
catch(ArithmeticException error1){
//处理方法
System.err.println("运算错误,除数不能为0!");
}catch(InputMismatchException error2){
System.err.println("请输入正确的数字!");
}
System.out.println("程序运行结束!");
}
}结果验证:
结果一:
请输入第一个数字:
123
请输入第二个数字:
123
1
程序运行结束!
结果二:
请输入第一个数字:
123
请输入第二个数字:
b
请输入正确的数字!
程序运行结束!
结果三:
请输入第一个数字:
123
请输入第二个数字:
0
运算错误,除数不能为0!
程序运行结束!
3、示例三
// 用户输入字符串转整型
3.1
public class Error {
String str;
public Error(String str) {
this.str = str;
}
public String Transform(){
try{
int num = Integer.parseInt(str);
}catch(NumberFormatException num){
System.out.println("字符串转整型,请输入正确的数字:");
}catch(Exception e){
}
return str;
}
}
// 编写测试类,调用数据类型转换方法,分别传递参数“Good!”、20
public class ErrorDemo {
public static void main(String[] args) {
Error er = new Error("Good!");
er.Transform();
System.out.println(er.str);
}
}
字符串转整型,请输入正确的数字:
Good!
3.2
立即学习“Java免费学习笔记(深入)”;
public class Error {
int num = 0;
public Error() {
}
public Error(int num) {
this.num = num;
}
public int TransformtoInt(String str){
try{
int num1 = Integer.parseInt(str);
}catch(NumberFormatException num){
System.err.println("字符串转整型,请输入正确的数字:");
}catch(Exception error){
error.printStackTrace();
}
return num;
}
}
基于Asp.Net+C#+Access的网上商店系统,具有智能化、高扩展、稳定、安全等特性,并拥有超强功能,可自由添加频道,后台智能修改风格,只要懂得网站常识的站长就可以轻松利用易想商城建立起专业的大型网上书店,点卡店、鲜花店、手机店、服装店、团购网等不同类型商城。易想商城有CMS增加频道功能,能够容易的把商城系统扩展成资讯网站多风格自由切换,全站经过专业的优化处理,让你的网站在百度上轻易的就能找
0
import java.util.Scanner;
public class ErrorDemo {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入一个数字:");
String str = input.next();
Error toInt = new Error();
toInt.TransformtoInt(str);
System.out.println(str);
}
}验证:
请输入一个数字:
123
123
请输入一个数字:
abc
字符串转整型,请输入正确的数字:
abc
4、示例四
//[b]throws、throw抛出异常[/b]
public class Error {
String sex ;
public Error() {
}
public String getSex() {
return sex;
}
public void setSex(String sex) throws Exception {
if(sex.equals("男")|sex.equals("女")){
this.sex = sex;
}else{
throw new Exception("性别必须为男或者女!");
}
}
}public class ErrorDemo {
public static void main(String[] args) {
Error er = new Error();
try{
er.setSex("熊");
}catch(Exception error){
error.printStackTrace();
}
System.out.println("程序结束");
}
}java.lang.Exception: 性别必须为男或者女!
at Error.setSex(Error.java:22)
at ErrorDemo.main(ErrorDemo.java:9)
程序结束
5、
自定义异常
// 创建Excption子类继承[b]Excption父类[/b]
//创建类
public class Error {
String sex ;
public Error() {
}
public String getSex() {
return sex;
}
public void setSex(String sex) throws Exception {
if(sex.equals("男")|sex.equals("女")){
this.sex = sex;
}else{
throw new ExceptionDemo("性别必须为男或者女!");
}
}
}//创建ExceptionDemo子类
public class ExceptionDemo extends Exception {
public ExceptionDemo() {
super();
}
public ExceptionDemo(String message) {
super(message);
}
}//创建测试类
import java.util.Scanner;
public class ErrorDemo {
public static void main(String[] args) {
Error er = new Error();
try{
Scanner next = new Scanner(System.in);
System.out.println("请输入性别:");
er.setSex(next.next());
}catch(Exception error){
error.printStackTrace();
}
System.out.println("程序结束!");
}
}结果验证:
请输入性别:
男
程序结束!
请输入性别:
nan ExceptionDemo: 性别必须为男或者女! at Error.setSex(Error.java:23) at ErrorDemo.main(ErrorDemo.java:10)
程序结束!
以上就是Java的异常处理 的内容,更多相关内容请关注PHP中文网(www.php.cn)!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号