【问题标题】:Scala continuation and exception handlingScala 延续和异常处理
【发布时间】:2011-05-27 09:30:16
【问题描述】:

假设,我想捕获一个异常,修复导致异常的问题并返回到发生异常的同一执行点继续。

如何在 Scala 中使用延续来实现它?这有意义吗?

【问题讨论】:

    标签: scala exception-handling continuations


    【解决方案1】:

    这是实现可恢复错误处理的一种可能方式:

    import java.io.File
    import java.lang.IllegalStateException
    import scala.util.continuations._
    
    // how it works
    
    ctry {
      println("start")
    
      val operationResult = someOperation(new File("c:\\ttttest"))
    
      println("end " + operationResult)
    } ccatch {
      case (DirNotExists(dir), resume) =>
        println("Handling error")
        dir.mkdirs()
        resume()
    }
    
    def someOperation(dir: File) = {
      cthrow(DirNotExists(dir))
      println(dir.getAbsolutePath + " " + dir.exists)
      "Operation finished"
    }
    
    // exceptions
    
    trait CException
    case class DirNotExists(file: File) extends CException
    
    // ctry/ccatch classes and methods
    
    sealed trait CTryResult[T] {
      def get: T
      def ccatch(fn: PartialFunction[(CException, () => T), T]): T
    }
    case class COk[T](value: T) extends CTryResult[T] {
      def ccatch(fn: PartialFunction[(CException, () => T), T]) = value
      def get = value
    }
    case class CProblem[T](e: CException, k: Any => Any) extends CTryResult[T] {
      def ccatch(fn: PartialFunction[(CException, () => T), T]) = 
              fn((e, () => k(Unit).asInstanceOf[T]))
      def get = throw new IllegalStateException("Exception was not processed: " + e)
    }
    
    def ctry[T](body: => T @cps[Any]) = reset (body) match {
      case (e: CException, k: (Any => Any)) => CProblem[T](e, k)
      case value => COk(value)
    }
    
    def cthrow(e: CException): Any @cps[Any] = shift((k: Any => Any) => (e, k))
    

    此代码产生以下输出:

    start 
    Handling error
    c:\ttttest true
    end Operation finished
    

    【讨论】:

      【解决方案2】:

      我曾经在 ruby​​ 中做过类似的事情。这只是一个测试,看看我是否可以在 ruby​​ 中实现 common lisp 的“可恢复异常”。你应该可以在 Scala 中做同样的事情,但我还没有尝试过。您的问题是关于一般概念还是关于实现细节?

      无论如何,这里是代码(没有保修;))

      #!/usr/bin/env ruby
      
      require 'continuation'
      
      #Module for adding elements of an array. Leaves error handling to the caller by using exceptions and continuations.
      module Adder
      
      #Exception class that offers continuations to the receiver.
          class CcExc < Exception
              def initialize(again, skip, index, sum)
                  @again = again
                  @skip = skip
                  @index = index
                  @sum = sum
              end
              def again
                  @again.call
              end
              def skip
                  @skip.call
              end
              attr_reader :index #where the problem occured
              attr_reader :sum #current sum   
          end
      
          #Method to get the current continuation
          def Adder.getcc
              cc = nil
              callcc {|c| cc = c}
              cc
          end
      
          #add all numbers in the array, raise an exception with continuations if an
          #item doesn't have the right type
          def Adder.addAll(array)
              sum = 0;
              array.each_with_index {|dummy,i|
                  again = getcc #save continuation before processing the item
                  if array[i].is_a? Numeric
                      sum += array[i] #process item normally
                  else
                      #raise exception with previously save continuation (again)
                      #and current continuation (skip)
                      callcc {|skip| raise CcExc.new again, skip, i, sum}
                  end
              }
              sum
          end
      end
      
      data = [1,"2",3,"hello",Object,"4",5,"END",6]
      begin
          puts "The sum is #{Adder.addAll data}."
      rescue Adder::CcExc => e
          puts "Exception raised."
          i = e.index
          case data[i]
              when /^\s*\d/
                  data[i] = data[i].to_i
                  puts 'Problem fixed. Continue adding.'
                  e.again
              when "END"
                  puts "'END' found. Stop processing."
                  puts "The sum is #{e.sum}"
              else
                  puts "'#{data[i]}' of type #{data[i].class} can't be converted " +
                       "to interger. Item skipped."
                  e.skip
          end
      end
      

      【讨论】:

        【解决方案3】:

        这个函数应该做的(把抛出异常的代码放在fooarg):

        def F[T](foo: => T, dealWithError: Exception => T): T =
          try foo
          catch{
            case ex: Exception => dealWithError(ex)}
        

        我使用这些类+隐式转换:

          class ORfoo[R](foo: () => R){
            def or(r: R): R =
              try foo() 
              catch{
                case ex: Exception => r
              }
            }
        

        implicit def ORfooWrapper[R](f: =&gt; R) = new ORfoo(() =&gt; f)

        它允许你进行类似 python 的异常处理,比如 "1a".toInt or 5

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-02-07
          • 2017-12-20
          • 2019-01-15
          • 2017-03-11
          • 1970-01-01
          • 2017-09-30
          • 1970-01-01
          相关资源
          最近更新 更多