[Scala] abnormal control

Scala exception handling There are two ways: catch the exception and thrown

Use the keyword throw throw an exception 

Use the keyword catch catch exceptions

Usually remind keyword finally ends abnormally

the ReadFile {Object 
    DEF main (as AGRS: Arry [String] { 
        the try { 
            Val = Source.fromFIle File ( "F.: //test.txt") 
            Val = file.getLines Lines () 
            for (Content <- Lines) { 
                Print ( Content) 
            } 
        } {the catch 
            Case EX: a FileNotFoundException => the println ( "input file does not exist" EX +) 
            Case EX: Exception => the println (EX) 
        } {the finally 
            prinln ( "commonly used to release resource") 
            } 
        } 
}

 

Guess you like

Origin www.cnblogs.com/guangluwutu/p/10966604.html