11_异常处理
2019-05-22 06:28:42来源:博客园 阅读 ()
public class Test01 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入被除数:"); int num1, num2; if (sc.hasNextInt()) { num1 = sc.nextInt(); System.out.println("请输入除数:"); if (sc.hasNextInt()) { num2 = sc.nextInt(); if (0 == num2) { System.out.println("除数不能为0"); } else { int r = num1 / num2; System.out.println("r=" + r); } } else { System.out.println("除数输入有误!"); } } else { System.out.println("被除数输入有误!"); } } }
public class Test01 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入被除数:"); int num1, num2; try { num1 = sc.nextInt(); System.out.println("请输入除数:"); num2 = sc.nextInt(); int r = num1 / num2; System.out.println("r = " + r); } catch (Exception e) { System.out.println("oops,产生异常:"+e); } System.out.println("程序结束..."); } }
try { num1 = sc.nextInt(); System.out.println("请输入除数:"); num2 = sc.nextInt(); int r = num1 / num2; System.out.println("r = " + r); } catch (InputMismatchException e) { System.out.println("产生输入不匹配异常:"+e); } catch (ArithmeticException e) { System.out.println("产生数学计算异常:"+e); } catch(Exception e) { System.out.println("通用异常:"+e); }
java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:864) at java.util.Scanner.next(Scanner.java:1485) at java.util.Scanner.nextInt(Scanner.java:2117) at java.util.Scanner.nextInt(Scanner.java:2076) at cn.exception02.Test02.main(Test02.java:14)
public class Test01 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); try { sc.nextInt(); } catch (Exception e) { e.printStackTrace(); } finally { System.out.println("finally 代码块"); } System.out.println("程序结束"); } }
try { // 打开数据库 // 增删改查 } catch (Exception e) { // 异常处理 } finally { // 关闭数据库 }
public static int add() { System.out.println("请输入第一个操作数:"); Scanner sc = new Scanner(System.in); int num1, num2; int r = 0; try { num1 = sc.nextInt(); System.out.println("请输入第二个操作数:"); num2 = sc.nextInt(); r = num1 + num2; return r; } catch (Exception e) { System.out.println("出现异常."); return r; } finally { System.out.println("finally"); sc.close(); } }
public class Test02 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); try { sc.nextInt(); System.exit(0); } catch (Exception e) { e.printStackTrace(); } finally { System.out.println("finally 代码块"); } System.out.println("程序结束"); } }
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Scanner; public class Test01 { public static void main(String[] args) { // 运行时异常 /*Scanner sc = new Scanner(System.in); System.out.println("请输入1-3之间的数字:"); int num = sc.nextInt(); System.out.println(num);*/ // 检查时异常 try { SimpleDateFormat df = new SimpleDateFormat(); df.parse("123"); } catch (ParseException e) { e.printStackTrace(); } } }
public 返回值 方法名(形参) throws 异常类1,异常类2,…{ }
public static int div(int a,int b) throws ArithmeticException{ int r = a / b; return r; }
public class Test01 { public static int div(int a) throws ArithmeticException,InputMismatchException{ System.out.println("请输入第二个参数:"); Scanner sc = new Scanner(System.in); int b = sc.nextInt(); int r = a / b; return r; } public static void main(String[] args) { // 处理div声明的异常 try { Test01.div(10); } catch (InputMismatchException | ArithmeticException e) { e.printStackTrace(); } } }
public class Test02 { // 声明检查时异常 public static int div(int a) throws Exception{ System.out.println("请输入第二个参数:"); Scanner sc = new Scanner(System.in); int b = sc.nextInt(); int r = a / b; return r; } public static void main(String[] args) { try { Test02.div(10); } catch (Exception e) { e.printStackTrace(); } } }
import java.util.InputMismatchException; import java.util.Scanner; public class Test03 { // 声明检查时异常 public static int div(int a) throws Exception{ System.out.println("请输入第二个参数:"); Scanner sc = new Scanner(System.in); int b = sc.nextInt(); int r = a / b; return r; } public static void main(String[] args) throws Exception{ // 调用处也不知道如何处理,继续上抛异常 => 在main上声明异常。 Test03.div(10); } }
package cn.exception07; public class Father { public void showInfo() throws RuntimeException{ System.out.println("father:showInfo"); } }
package cn.exception07; public class Son extends Father{ @Override public void showInfo() { super.showInfo(); System.out.println("我是son:showInfo"); } }
public class Father { public void showInfo() { System.out.println("father:showInfo"); } }
@Override public void showInfo() throws RuntimeException{ super.showInfo(); System.out.println("我是son:showInfo"); }
public class Father { public void showInfo() throws Exception{ System.out.println("father:showInfo"); } }
public class Son extends Father{ @Override public void showInfo() { try { super.showInfo(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("我是son:showInfo"); } }
public class Son extends Father{ @Override public void showInfo() throws Exception { super.showInfo(); System.out.println("我是son:showInfo"); } }
public void setAge(int age) throws Exception{ if(age > 0) { this.age = age; }else { throw new Exception("年龄不合法"); } }
public class AgeException extends Exception{ public AgeException() { super(); } public AgeException(String message) { super(message); } public int showInfo() { System.out.println(this.getClass()+"=>"+super.getMessage()); return 0; } }
原文链接:https://www.cnblogs.com/aknife/p/10890097.html
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- Java笔记:数组,异常,泛型 2020-06-08
- Spring Cloud Gateway 全局通用异常处理 2020-06-08
- 聊聊 OAuth 2.0 的 Token 续期处理 2020-06-08
- 架构设计 | 异步处理流程,多种实现模式详解 2020-06-04
- SpringCloud异常处理统一封装我来做-使用篇 2020-05-23
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash