Guest User

Untitled

a guest
Aug 18th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. Scala exception signature definition using generics
  2. public interface BaseInterface<T, E extends Throwable> {
  3. public T process(Class<E> wrapperExc) throws E;
  4. }
  5.  
  6. public class ExcOne extends Exception {}
  7.  
  8. public class SubclassOne implements BaseInterface<String, ExcOne> {
  9. @Override
  10. public String process(Class<ExcOne> wrapperExc) throws ExcOne {
  11. return null;
  12. }
  13. }
  14.  
  15. class UsingGenerics[IN, OUT, E <: Throwable] {
  16.  
  17. //@throws(classOf[E]) - error: class type required but E found
  18. def process(request: IN, wrapperExc: Class[E]): OUT = {
  19. null.asInstanceOf[OUT]
  20. }
  21.  
  22. }
  23.  
  24. trait BaseTrait {
  25.  
  26. type Request
  27. type Response
  28. type BusinessException <: Throwable
  29.  
  30. //error: class type required but BaseTrait.this.BusinessException found
  31. //@throws(classOf[BusinessException])
  32. def process(request: Request): Response
  33.  
  34. }
  35.  
  36. class TraitImplementor extends BaseTrait {
  37.  
  38. type Request = Input
  39. type Response = Output
  40. type BusinessException = BizExc
  41.  
  42. def process(r: Request): Response = {
  43. if (1 != 2) throw new BusinessException("Bang")
  44. new Response
  45. }
  46.  
  47. }
  48.  
  49. class Input
  50. class Output
  51. class BizExc(msg: String) extends Exception(msg)
  52.  
  53. public @interface Throwing { Class<? extends Throwable> exceptionType(); }
Add Comment
Please, Sign In to add comment