您的位置:首页 > 其它

[0.3] scala函数定义、流程控制与异常处理

2016-06-03 09:27 148 查看

场景

scala语法基础实战

知识点

标准函数定义

def funName(args:T*):returnType={... returnValue}


流程控制

if-else、while 与 for


异常处理

try { ... } catch { ... } finally{ ... }


实验

函数定义

/**
* 函数定义语法:
* def funName(arg:argType,...)[:returnType] =
* {
*    [returnValue]
* }
*/
def looper(x:Long,y:Long):Long =
{
var a = x
var b = y
while(a!=0)
{
val temp = a
a = b%a
b = temp
}
b
}


流程控制

/**
* 条件控制 if-else
*/
def testIf(x:Int) =
{
//语法一
var file = "scala.java"
if(0.equals(x)) file = "scala.scala" else "spark.scala"
println(file)

//语法二
var file2 = if(0.equals(x)) "scala.scala" else "spark.scala"
println(file2)
}

/**
* 循环控制 for : 变量不用显性定义可以直接使用
*/
def testFor() =
{
for(i <- 1 to 10)
{
println(i)
}

for(i <- 1.to(10))
{
println(i)
}

var files = (new java.io.File(".")).listFiles()
for(file <- files)
{
println(file)
}
}

/**
* 循环控制 do-while
*/
def testDowhile()
{
var line = ""
do
{
line = readLine()
println(line)
}
while(!"".equals(line) )
}


异常处理

/**
* 异常控制
* 语法:
* try{}catch{[case e:Exception =>]...}finally{}
*/
def testException(x:Int) =
{
try
{
var num = if(x%2 == 0) x%2 else throw new RuntimeException("Input should be event!")
println(num)
}
catch
{
case e:Exception => println(e.getMessage())
}
finally
{
//do-something
}
}
}


·语言级·spark源码初探

源码

package net.apache.spark

/**
* tags:语言级spark源码初探
* function:try-catch-finally 在SparkContext类中的应用
*
*关于SparkContext类: “Main entry point for Spark functionality. A SparkContext represents the connection to a Spark
* cluster, and can be used to create RDDs, accumulators and broadcast variables on that cluster.
*
* Only one SparkContext may be active per JVM.  You must `stop()` the active SparkContext before
* creating a new one.  This limitation may eventually be removed; see SPARK-2243 for more details.”
*
*/
class SparkContext(conf:SparkConf)  extends Logging with ExecutorAllocationClient{
try {
if (!_conf.contains("spark.master")) {
throw new SparkException("A master URL must be set in your configuration")
}
if (!_conf.contains("spark.app.name")) {
throw new SparkException("An application name must be set in your configuration")
}
} catch {
case NonFatal(e) => logError("Error initializing SparkContext.", e)
try {
stop()
} catch {
case NonFatal(inner) =>
logError("Error stopping SparkContext after init error.", inner)
} finally {
throw e
}
}
}


解读

实例化SparkContext时会对用户提交的应用程序及相关参数进行校验:如果用户没有指定spark.master等参数的值,则向外层抛出相关异常.

总结

经典异常处理结构

try { ... } catch { case ObjectException ... } finally{ ... }


参考

王家林DT大数据梦工厂

spark1.6.0源码
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: