Skip to content

Commit

Permalink
exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
novemart committed Nov 11, 2022
1 parent bf2c056 commit 15b7b9a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
45 changes: 45 additions & 0 deletions Scala/Day5/Friday.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import java.io.{FileNotFoundException, FileReader, IOException}
import java.nio.file.AccessDeniedException
import scala.io.Source
import scala.util.{Failure, Success, Try}

@throws(classOf[RuntimeException])
def half(n:Int): Int =
if n%2 == 0 then
n/2
else
throw new RuntimeException("n must be even")

def workWithFile(path:String) ={
// try
// for (line <- Source.fromFile(path).getLines){
// println(line)
// }
Try{ Source.fromFile(path).getLines.mkString}

// catch
// case ex: FileNotFoundException =>println("Where is the file?")
// case ex: AccessDeniedException => println("Oh oh")
}



object Friday {
def main(args: Array[String]): Unit = {

//println(half(3))

try
print(half(3))
catch
case ex: RuntimeException => println("Well that didn't go well")
// println(half(2))

val result = workWithFile("src/main/scala/files/text.txt")
result match{
case Success(value) => println(value)
case Failure(e) => println(e.getMessage())
}

}
}
3 changes: 3 additions & 0 deletions Scala/Day5/files/text.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
What do you call a fish wearing a bowtie?

Sofishticated!

0 comments on commit 15b7b9a

Please sign in to comment.