Kotlin - 33. 이상 (예외)

공식 문서:http://kotlinlang.org/docs/reference/exceptions.html
1. 예외 클래스 (예외 클래스)
 java  ,Kotlin        Throwable     (    Throwable ),
           (message),    (stack trace)      (optional cause).

1. Java  ,kotlin  throw       (throw exception):
    throw MyException("Hi There!")

2. Java  ,kotlin  try       (catch exceptio):
    try {
        //     
    } catch (e: SomeException) {
        //     
    } finally {
        //    finally 
    }
    //       catch ,finally    , catch finally         !

3. Java  ,kotlin try         
    try       try        ,   catch        ,
    finally             .
    fun main(args: Array) {
        val a: Int? = try { 
                1 //    ,  1               
            } catch (e: NumberFormatException) {
                2
                null
            }  finally {
                3 //finally          
            }

        val b: Int? = try { 
                1
                throw NumberFormatException()
            } catch (e: NumberFormatException) {
                2
                null //    ,  null
            }  finally {
                3 //finally          
            }

        println(a) //  1
        println(b) //  null
    }

2. 검사 이상 없 음 (Checked Exceptions)
java      :     (checked exception)      (unchecked exception)
    1.Error RuntimeException           (unchecked exception);
    2.     Exception      (checked exception)。
             ,                    ,
        try catch         ,         !

      (Checked Exceptions)      ,     ,     java API     ,
              ,              ,
     Java     ,      (Checked Exception)             !

kotlin      (Checked Exceptions),   kotlin            :
    JDK StringBuilder          :
        Appendable append(CharSequence csq) throws IOException;
      append    throws IOException,         (StringBuilder/    log/   console),
         IOException(      IO  Writer    Appendable),    try{}      :
        try {
            log.append(message)
        } catch (IOException e) {

        }
    java     (Checked Exception)    ,  《Effective Java》 65 :      !
    
    Bruce Eckel 《Java         ?》Does Java need Checked Exceptions?   :
                     :                     ,
                         :     ,          !

          :
        《Java          》Java's checked exceptions were a mistake (Rod Waldhoff)
        《       》The Trouble with Checked Exceptions (Anders Hejlsberg)

3. 아무것도 유형 (아무것도 유형)
 Kotlin throw    ,      Elvis   ?:    :
    val s = person.name ?: throw IllegalArgumentException("Name required")

throw       Nothing  ,        ,               (never be reached)
   person.name null , s =           ( throw  Nothing,      s = )

     Nothing              (never return):
    fun main(args: Array) {
        fun fail(message: String): Nothing {
            throw IllegalArgumentException(message)
        }
        
        //      fail() ,               (             )
        //    ,   "java.lang.IllegalArgumentException: Name    ,   null"
        val name = null  
        val s: String = name ?: fail("Name    ,   null")
        println(s)
    }

4. kotlin 과 자바 가 서로 작 동 하 는 이상 처리 (자바 Interoperability)
 Kotlin ,          ,                (try catch)! 
  , Kotlin          Java  ,          :   
    //kotlin  ,  java  ,append(CharSequence csq) throws IOException;     
    fun render(list: List, to: Appendable) {
        for (item in list) {
            // kotlin        ,  Java        IOException
            to.append(item.toString()) 
        }
    }

:http://www.jianshu.com/p/227f398b929f CSDN 블 로그:http://blog.csdn.net/qq_32115439 / article / details / 74617358 GitHub 블 로그:http://lioil.win/2017/07/06/Kotlin-exception.html 코딩 블 로그:http://c.lioil.win/2017/07/06/Kotlin-exception.html

좋은 웹페이지 즐겨찾기