Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3456154
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:42:05+00:00 2026-05-18T09:42:05+00:00

Suppose I want to wrap code that can throw exceptions with a try-catch block

  • 0

Suppose I want to wrap code that can throw exceptions with a try-catch block that logs the exception and continues. Something like:

loggingExceptions {
  // something dangerous
}

Ideally, I would like to use for logging the Logger defined on the calling object, if any (and if none, get a compile-time error). I’d love to define something like this:

def loggingExceptions[L <: { def logger: Logger }](work: => Unit)(implicit objectWithLogger: L): Unit = {
  try {
    work
  } catch {
    case t: Exception => objectWithLogger.logger.error(t.getMessage)
  }
}

where objectWithLogger would somehow “magically” expand to “this” in client code. Is this (or a similar thing) possible?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T09:42:06+00:00Added an answer on May 18, 2026 at 9:42 am

    It can in fact be done just as you want. The other answerers surrendered too quickly. No white flags!

    package object foo {
      type HasLogger = { def logger: Logger }
      implicit def mkLog(x: HasLogger) = new {
        def loggingExceptions(body: => Unit): Unit =
          try body
          catch { case ex: Exception => println(ex) }
      }
    }
    
    package foo {
      case class Logger(name: String) { }
    
      // Doesn't compile:
      // class A {
      //   def f = this.loggingExceptions(println("hi"))
      // }
      // 1124.scala:14: error: value loggingExceptions is not a member of foo.A
      //         def f = this.loggingExceptions(println("hi"))
      //                      ^
      // one error found  
    
      // Does compile
      class B {
        def logger = Logger("B")
        def f = this.loggingExceptions(println("hi"))
        def g = this.loggingExceptions(throw new Exception)
      }
    }
    
    object Test {
      def main(args: Array[String]): Unit = {
        val b = new foo.B
        b.f
        b.g
      }
    }
    
    // output
    //
    // % scala Test
    // hi
    // java.lang.Exception
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose using .wrap() can wrap a tag on a element, however I want to
Suppose I want to put objects that identify a server into a stl set
Suppose I want to execute code, for example value += 5 inside a namespace
Suppose you have a point cloud, and you want a surface that wraps those
Suppose I have an HTML page that looks something like this: <html><body> 00123 <input
Suppose I want to completely take over the open() system call, maybe to wrap
Suppose I want to add two buffers and store the result. Both buffers are
Suppose I want to store N samples (each sample takes up a significant portion
Suppose I want to have a blog with Rails 3 on my website and
Suppose I want to make a Web application which uses a fixed width and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.