Executing a code block even after catching an exception in Scala

noob101

In my current method, I am trying to make a series of calls and if any of them fail, I want to be able to continue running the remainder (while capturing the Exception that was thrown). I am having a hard time figuring this out in Scala.

So in this example, I want to kick off each of these calls - RunA, RunB and RunC but if RunB throws an exception, I want to print that and continue kicking off RunC after that.

var result = Try {
    new RunA()
    new RunB()
    new RunC()
}   catch { 
    case e: Throwable => e.printStackTrace()
    false
 }

Outside of having them all individually wrapped in a Try/Catch, I am sure there are better ways to do this which is why I am hoping someone can help with this.

I looked at the 'Ignoring' exception but it appears to completely ignore the exception which I want to atleast log.

Thanks!

Rex Kerr

First, don't mix try { ... } catch { ... } up with scala.util.Try{ ... }.

You can

import scala.util._
val runA = Try{ new RunA }
val runB = Try{ new RunB }
val runC = Try{ new RunC }

and then deal with the exceptions as you see fit. For instance, if you want to print and continue, you could deal with the try statements right there:

def getOrPrint[A](f: => A): Option[A] = Try{ f } match {
  case Success(x) => Some(x)
  case Failure(e) => e.printStackTrace; None
}

getOrPrint{ new RunA }
...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Resume code after catching exception

can we throw the exception object to calling method even after catching it in the catch block?

Catch block executing even when try block throws no exception?

Executing code after exception is thrown

How do I move back to try block after catching exception

How can I continue executing code after a try/catch block catches an exception?

ComponentDidMount Code Executing even after component unmounts

Exception not catching in try/catch block

Why is code in PLSQL not executing after "exception" part?

Getting selenium python to run previous line of code after catching an exception

Catching exception in Play for Scala method

Executing side effect code inside scala for-comprehension block

Scala: Warn if block of code is executing for more than certain duration?

Catching multiple exception types in one catch block

@try@catch block not catching inside exception

Except block not catching exception in ipython notebook

Catching inner exception in F# async block

Nested Try-Catch Block Not Catching Exception

executing block of code atomically

Reraise (same exception) after catching an exception in Ruby

I'm getting an Array out of bounds exception after executing this code:

Why is the IF statement still executing after it code block falls into the ELSE code block?

Scala try-catch not catching exception

Node.js UnhandledPromiseRejectionWarning even after catching it

object is not destroyed after catching exception in mixed C and C++ code programming

Behavior of executing a method call with a checked exception at runtime without catching it

PHP not executing after try block

How to exit program execution after catching exception

Trouble with for loop continuing after catching an exception