try(exp)//运行exp
catch{
case exp1=>exp2}//出现exp1时干啥
throw 表示抛出错误的意思
case classCustomer(age: Int)
class Cigarettescase class UnderAgeException(message:String) extends Exception(message)
def buyCigarettes(customer: Customer): Cigarettes =
if (customer.age < 16)
throw UnderAgeException(s"Customer must be older than 16 but was ${customer.age}")
else new Cigarettes
valyoungCustomer = Customer(15)
try {
buyCigarettes(youngCustomer)
"Yo, here are your cancer sticks! Happy smokin'!"
} catch {
case UnderAgeException(msg) => msg
}