動手動腦4

import java.io.*;
public class ThrowMultiExceptionsDemo { public static void main(String[] args) { try { throwsTest(); } catch(IOException e) { System.out.println("捕捉異常"); }}private static void throwsTest()  throws ArithmeticException,IOException { System.out.println("這只是一個測試"); // 程序處理過程假設發生異常throw new IOException(); //throw new ArithmeticException(); 
    } 
}
public class CatchWho { public static void main(String[] args) { try { try { throw new ArrayIndexOutOfBoundsException(); } catch(ArrayIndexOutOfBoundsException e) { System.out.println(  "ArrayIndexOutOfBoundsException" +  "/內層try-catch"); }throw new ArithmeticException(); } catch(ArithmeticException e) { System.out.println("發生ArithmeticException"); } catch(ArrayIndexOutOfBoundsException e) { System.out.println(  "ArrayIndexOutOfBoundsException" + "/外層try-catch"); } } 
}
CatchWho2.java
public class CatchWho2 { public static void main(String[] args) { try {try { throw new ArrayIndexOutOfBoundsException(); } catch(ArithmeticException e) { System.out.println( "ArrayIndexOutOfBoundsException" + "/內層try-catch"); }throw new ArithmeticException(); } catch(ArithmeticException e) { System.out.println("發生ArithmeticException"); } catch(ArrayIndexOutOfBoundsException e) { System.out.println( "ArrayIndexOutOfBoundsException" + "/外層try-catch"); } } 
}
CheckedExceptionDemo.java
import java.io.*; public class CheckedExceptionDemo { public static void main(String[] args)  { try { BufferedReader buf = new BufferedReader( new InputStreamReader(System.in));    //拋出受控的異常System.out.print("請輸入整數: "); int input = Integer.parseInt(buf.readLine()); //有可能引發運行時異常System.out.println("input x 10 = " + (input*10)); } //以下異常處理語句塊是必須的,否則無法通過編譯catch(IOException e) { System.out.println("I/O錯誤"); } //以下異常處理語句塊可以省略,不影響編譯,但在運行時出錯catch(NumberFormatException e) { System.out.println("輸入必須為整數"); }} 
}
EmbededFinally.javapublic class EmbededFinally {public static void main(String args[]) {int result;try {System.out.println("in Level 1");try {System.out.println("in Level 2");// result=100/0;  //Level 2try {System.out.println("in Level 3");result=100/0;  //Level 3
                } catch (Exception e) {System.out.println("Level 3:" + e.getClass().toString());}finally {System.out.println("In Level 3 finally");}// result=100/0;  //Level 2
}catch (Exception e) {System.out.println("Level 2:" + e.getClass().toString());}finally {System.out.println("In Level 2 finally");}// result = 100 / 0;  //level 1
        } catch (Exception e) {System.out.println("Level 1:" + e.getClass().toString());}finally {.             System.out.println("In Level 1 finally");}}}
ExceptionLinkInRealWorld.java
/*** 自定義的異常類* @author JinXuLiang**/
class MyException extends Exception
{public MyException(String Message) {super(Message);}public MyException(String message, Throwable cause) {super(message, cause);}public MyException( Throwable cause) {super(cause);}}public class ExceptionLinkInRealWorld {public static void main( String args[] ){try {throwExceptionMethod();  //有可能拋出異常的方法調用
      }catch ( MyException e ){System.err.println( e.getMessage() );System.err.println(e.getCause().getMessage());}catch ( Exception e ){System.err.println( "Exception handled in main" );}doesNotThrowException(); //不拋出異常的方法調用
   }public static void throwExceptionMethod() throws MyException{try {System.out.println( "Method throwException" );throw new Exception("系統運行時引發的特定的異常");  // 產生了一個特定的異常
      }catch( Exception e ){System.err.println("Exception handled in method throwException" );//轉換為一個自定義異常,再拋出throw new MyException("在方法執行時出現異常",e);}finally {System.err.println("Finally executed in throwException" );}// any code here would not be reached
   }public static void doesNotThrowException(){try {System.out.println( "Method doesNotThrowException" );}catch( Exception e ){System.err.println( e.toString() );}finally {System.err.println("Finally executed in doesNotThrowException" );}System.out.println("End of method doesNotThrowException" );}
}
ExplorationJDKSource.javapublic class ExplorationJDKSource {/*** @param args*/public static void main(String[] args) {System.out.println(new A());}}class A{}
OverrideThrows.javaimport java.io.*;public class OverrideThrows
{public void test()throws IOException{FileInputStream fis = new FileInputStream("a.txt");}
}
class Sub extends OverrideThrows
{//如果test方法聲明拋出了比父類方法更大的異常,比如Exception//則代碼將無法編譯……public void test() throws FileNotFoundException{//...
    }
}+
// UsingExceptions.java
// Demonstrating the getMessage and printStackTrace
// methods inherited into all exception classes.
public class PrintExceptionStack {public static void main( String args[] ){try {method1();}catch ( Exception e ) {System.err.println( e.getMessage() + "\n" );e.printStackTrace();}}public static void method1() throws Exception{method2();}public static void method2() throws Exception{method3();}public static void method3() throws Exception{throw new Exception( "Exception thrown in method3" );}
}public class SystemExitAndFinally {public static void main(String[] args){try{System.out.println("in main");throw new Exception("Exception is thrown in main");//System.exit(0);
}catch(Exception e){System.out.println(e.getMessage());System.exit(0);}finally{System.out.println("in finally");}}}
ThrowDemo.java
public class ThrowDemo { public static void main(String[] args) { 
//        try {double data = 100 / 0.0;System.out.println("浮點數除以零:" + data); 
//            if(String.valueOf(data).equals("Infinity"))
//            { 
//                System.out.println("In Here" ); 
//                throw new ArithmeticException("除零異常");
//            }
//        } 
//        catch(ArithmeticException e) { 
//            System.out.println(e); 
//        } 
    } 
}
import java.io.*;
public class ThrowMultiExceptionsDemo { public static void main(String[] args) { try { throwsTest(); } catch(IOException e) { System.out.println("捕捉異常"); }}private static void throwsTest()  throws ArithmeticException,IOException { System.out.println("這只是一個測試"); // 程序處理過程假設發生異常throw new IOException(); //throw new ArithmeticException(); 
    } 
}

?

轉載于:https://www.cnblogs.com/sljslj/p/9944151.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/250267.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/250267.shtml
英文地址,請注明出處:http://en.pswp.cn/news/250267.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

javascript --- 對象原型

對象原型 參考 - MDN Javascript中的原型 在Javascript中,每一個函數都有一個特殊的屬性,叫做原型 下面獲取函數的原型fn.prototype function f1(){} console.log(f1.prototype) /*{constructor: f f1()__proto__:{constructor: f Object()__defineGetter__: f __defineGe…

從零認識單片機(9)

keil軟件: IDE:IDE是集成開發環境,就是用來開發的完整的軟件系統。 keil和mdk: keil:只能用來開發單片機 mdk:基于keil 拓展ARM的開發,主要用來開發ARM-cortex-m系列單片機的程序。 使用keil打開已有的工程項目: 1、IDE開發軟件&a…

javascript --- vue2.x中原型的使用(攔截數組方法) 響應式原理(部分)

說明 在Vue2.x中,利用了對原型鏈的理解,巧妙的利用JavaScript中的原型鏈,實現了數組的pop、push、shift、unshift、reverse、sort、splice等的攔截. 你可能需要的知識 參考 - MDN 原型鏈 JavaScript常被描述為一種基于原型的語言(prototype-based language),每個對象擁有一…

dubbo-admin構建報錯

dubbo-admin構建報錯 意思是maven庫里沒有dubbo2.5.4-SNAPSHOT.jar這個版本的dubbo的jar包&#xff0c;把dubbo-admin項目的pom.xml的   <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>${proje…

javascript --- 手寫Promise、快排、冒泡、單例模式+觀察者模式

手寫promise 一種異步的解決方案, 參考 Promise代碼基本結構 function Promise(executor){this.state pending;this.value undefined;this.reason undefined;function resolve(){}function reject(){} } module.exports Promisestate保存的是當前的狀態,在Promise狀態發…

PyCharm 通過Github和Git上管理代碼

1.Pycharm中設置如圖: 2.配置Git,通過網頁 https://www.git-scm.com/download/win 下載 3. 轉載于:https://www.cnblogs.com/0909/p/9956406.html

【BZOJ】2395: [Balkan 2011]Timeismoney

題解 最小乘積生成樹&#xff01; 我們把&#xff0c;x的總和和y的總和作為x坐標和y左邊&#xff0c;畫在坐標系上 我們選擇兩個初始點&#xff0c;一個是最靠近y軸的A&#xff0c;也就是x總和最小&#xff0c;一個是最靠近x軸的B&#xff0c;也就是y總和最小 連接兩條直線&…

http --- http與https相關概念小結

網絡協議 參考 HTTP的特性 HTTP協議構建于TCP/IP協議之上,是一個應用層協議,默認端口是80HTTP是無連接無狀態的 HTTP報文 請求報文 HTTP協議是以ASCII碼傳輸,建立在 TCP/IP 協議之上的應用層規范。規范把HTTP請求分為三個部分:狀態行、請求頭、消息主體。 <method>…

Spring AOP注解方式實現

簡介 上文已經提到了Spring AOP的概念以及簡單的靜態代理、動態代理簡單示例&#xff0c;鏈接地址&#xff1a;https://www.cnblogs.com/chenzhaoren/p/9959596.html 本文將介紹Spring AOP的常用注解以及注解形式實現動態代理的簡單示例。 常用注解 aspect&#xff1a;定義切面…

享元模式-Flyweight(Java實現)

享元模式-Flyweight 享元模式的主要目的是實現對象的共享,即共享池,當系統中對象多的時候可以減少內存的開銷,通常與工廠模式一起使用。 本文中的例子如下: 使用享元模式: 小明想看編程技術的書, 就到家里的書架上拿, 如果有就直接看, 沒有就去買一本, 回家看. 看完了就放到家里…

算法 --- 回溯法

回溯法 參考 - 劍指Offer 回溯法可以看成蠻力法的升級版,它從解決問題每一步的所有可能選項里系統地選擇出一個可行的解決方案. 回溯法解決的問題的特性: 可以形象地用樹狀結構表示: 節點: 算法中的每一個步驟節點之間的連接線: 每個步驟中的選項,通過每一天連接線,可以到達…

013.Zabbix的Items(監控項)

一 Items簡介 Items是從主機里面獲取的所有數據&#xff0c;可以配置獲取監控數據的方式、取值的數據類型、獲取數值的間隔、歷史數據保存時間、趨勢數據保存時間、監控key的分組等。通常情況下item由key參數組成&#xff0c;如監控項中需要獲取cpu信息&#xff0c;則需要一個對…

Cookie 和 Session的區別

pass 下次再寫轉載于:https://www.cnblogs.com/nieliangcai/p/9073520.html

算法 --- 記一道面試dp算法題

題目: 給定一個數組(長度大于1),如下 let a [1,4,3,4,5] // 長度不確定,數值為整數要求寫一個函數,返回該數組中,除本身數字之外其他元素的成積.即返回如下: // 過程[4*3*4*5, 1*3*4*5, 1*4*4*5, 1*4*3*5, 1*4*3*4] // 結果[240, 60, 80, 60, 48]題目要求不使用除法,且時間…

編碼

一、什么是編碼&#xff1f;首先&#xff0c;我們從一段信息即消息說起&#xff0c;消息以人類可以理解、易懂的表示存在。我打算將這種表示稱為“明文”&#xff08;plain text&#xff09;。對于說英語的人&#xff0c;紙張上打印的或屏幕上顯示的英文單詞都算作明文。其次&a…

ASP.NET MVC 實現頁落網資源分享網站+充值管理+后臺管理(10)之素材管理

源碼下載地址&#xff1a;http://www.yealuo.com/Sccnn/Detail?KeyValuec891ffae-7441-4afb-9a75-c5fe000e3d1c 素材管理模塊也是我們這個項目的核心模塊&#xff0c;里面的增刪查改都跟文章管理模塊相同或者相似&#xff0c;唯一不同點可能是對附件的上傳處理&#xff0c;但…

javascript --- [express+ vue2.x + elementUI]登陸的流程梳理

說明 涉及到以下知識點: 登陸的具體流程express、vue2.x、elementUI、axios、jwt、assert 登陸方面的API使用中間件的使用前后端通過http狀態碼,進行響應的操作(這里主要是401)密碼驗證(bcrypt的hashSync方法對明文密碼進行加密,compareSync方法對加密的密碼進行驗證)token的…

設計模式---裝飾模式

今天學習了裝飾模式&#xff0c;做個筆記。。裝飾模式的基礎概念可以參考&#xff1a;https://blog.csdn.net/cjjky/article/details/7478788 這里&#xff0c;就舉個簡單例子 孫悟空有72變&#xff0c;但是它平時是猴子&#xff0c;遇到情況下&#xff0c;它可以變成蝴蝶等等 …

springMvc 注解@JsonFormat 日期格式化

1&#xff1a;一定要加入依賴,否則不生效&#xff1a; <!--日期格式化依賴--><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>${jackson.version}</version>&…

Git很簡單--圖解攻略

Git Git 是目前世界上最先進的分布式版本控制系統&#xff08;沒有之一&#xff09;作用 源代碼管理為什么要進行源代碼管理? 方便多人協同開發方便版本控制Git管理源代碼特點 1.Git是分布式管理.服務器和客戶端都有版本控制能力,都能進行代碼的提交、合并、. 2.Git會在根…