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 6728241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:08:46+00:00 2026-05-26T10:08:46+00:00

I started learning Scala yesterday, so I’m pretty new to it. One thing I

  • 0

I started learning Scala yesterday, so I’m pretty new to it. One thing I like to do when learning a new language is trying to create a micro-TDD lib.

This is what I got so far:

def assert(condition: Boolean, message: String) {
  if(!condition){ throw new AssertionError(message) }
}

def assertThrows[E](f: => Unit) {
  try {
    f
  } catch {
    case e: E => { return }
    case _: Exception => { }
  }
  throw new AssertionError("Expected error of type " + classOf[E] )
}

The code for assert works just fine, but I’m having two problems with assertThrows.

  • It seems that I can’t use E on the last line. No matter what I do, I get a class type expected but E found error.
  • If I remove E from the last line (replacing it by throw new AssertionError("error expected"), for example) I get this: warning: abstract type E in type pattern is unchecked since it is eliminated by erasure

I think that the two problems I’m having are related with the way Scala (and probably java) deals with abstract types, and how are they done.

How can I fix my assertThrows?

Bonus points: is the way I’m specifying a “block type” (f: => Unit) correct ?

  • 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-26T10:08:47+00:00Added an answer on May 26, 2026 at 10:08 am

    The Java virtual machine implements generics through type erasure, so inside the method body the JVM doesn’t actually know anything about what type E is, so this AssertThrows method can’t work the way you’d like. You need to implicitly pass a Manifest for your exception class, like so:

    def assertThrows[E](f: => Unit)(implicit eType:ClassManifest[E]) {
    

    Then you can use it in the body to catch the exception or get the class name like so:

      try {
        f
      } catch {
        case e: Exception =>
          if ( eType.erasure.isAssignableFrom(e.getClass))
            return;
      }
      throw new AssertionError("Expected error of type " + eType.erasure.getName )
    }
    

    Thanks to the Spring framework’s AssertThrows class for showing me how to do this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just recently started learning the Scala language and would like to do it
I'm new to Scala ,just started learning it today.I would like to know how
I am new to Scala, just started learning, so this is basic beginner question.
I started learning jQuery just yesterday, and I like it very much. And now
I've just started learning Scala, and the first thing I'm going to implement is
Just started learning c++ today and im pretty boggled. its an amazing language but
I started learning JavaScript a while ago. It's a fairly easy programming language considering
I have just started learning Scala and I'm now wondering how I could implement
I started learning the scala actors framework about two days ago. To make the
So I started learning Scala today and I got all my vim config files

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.