zoukankan      html  css  js  c++  java
  • [Kotlin] Catch Error in Java

    For example you have the Java class:

    package com.rsk.java;
    import com.rsk.kotlin.Customer;
    import com.rsk.kotlin.CustomerDatabase;
    
    import java.util.*;
    
    public class Main {
        public static void main(String[] args) {
            Customer phil = new Customer(0, "Phil");
            CustomerDatabase db = new CustomerDatabase();
            List<Customer> customers = db.getCustomers();
            try {
                customers.add(phil);
            } catch (IllegalAccessException e) {
                System.out.println(e);
            }
        }
    }

    If you have used kotlin, need to add @Throw to tell java, that fun might throws:

    package com.rsk.kotlin
    
    data class Customer (val id: Long, val name: String)
    
    class CustomerDatabase() {
        val customers = listOf(
                Customer(1, "Matti"),
                Customer(2, "James"))
    
        @Throws(IllegalAccessException::class)
        fun addCustomer(c: Customer) {
            throw IllegalAccessException("You cannot add a customer")
        }
    }
  • 相关阅读:
    爬虫
    modelform
    验证码
    ajax
    ngnix和负载均衡
    django 补充和中间件
    django补充和form组件
    C常量与控制语句
    Web应用开发技术(3)-html
    Web应用开发技术(2)-html
  • 原文地址:https://www.cnblogs.com/Answer1215/p/13850070.html
Copyright © 2011-2022 走看看