Skip to content

Conversation

@hamzaremmal
Copy link
Member

This PR improve the implementation of safer exceptions as introduced in Scala 3.1 (see PR #11721 by @odersky).

The suggested implementation completes the safer exceptions project by adding missing checks. The following changes have been added:

  • Changes to the syntax to allow throws clauses in function definition. When enabling the safer exceptions feature, the following function definition is allowed : def foo throws Exception = ???

  • Fix overriding problems when functions throws exceptions

  • Drop contextual parameter lists to bring a CanThrow capabilities into a certain scope

  • Favor contextual functions and union types when desugaring throws clauses

  • Change the semantics of implicit search of CanThrow capabilities

  • Add to the bytecode the exceptions a function might throw

  • Require a CanThrow capability for all function annotated with the @throws annotation

  • Add override checks for methods with @throws annotations

  • Implement special handling for CanThrow capabilities for main-methods

  • Change the semantics of the $throws infix type to reflect the changes

  • Add "ghost" capabilities to the corresponding capture set

  • Syntax changes - throws clauses : When enabled, the following code is accepted

import scala.language.experimental.saferExceptions

class A extends Exception
class B extends Exception

// New syntax to declare checked exceptions
def foo throws A, B = ???

// Declare checked exceptions using $throws infix type
def foo : Unit throws A | B = ???

// Desugared representation of both syntaxes
def foo : CanThrow[A | B] ?=> Unit = ???
  • @throws annotation : The following function declarations are equivalent
@throws[Exception]
def foo : Unit = ???

def foo throws Exception : Unit = ???

Please note that this PR is part of my semester project in @lampepfl. The project was done under the supervison of @abgruszecki and @odersky.

@hamzaremmal
Copy link
Member Author

This PR should close issue #15510

@tgodzik
Copy link
Contributor

tgodzik commented Jun 18, 2025

@hamzaremmal do you plan to work on this again? Could we close it for now otherwise?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants