Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call Super Constructor

Tags:

I have a custom exception class like the following :

case class CustomException(errorMsg:String)  extends Exception(error:String) 

All what I need that when I catch exception is to throw my custom exception and pass my error message to the custom exception . I expect from CustomException constructor to call super(errMsg) However , this isn't what goes now and I got a compilation error .

 catch {       case s: Exception => throw CustomException("This is a custom message")     } 

How could I call the super constructor :

super(errorMessage) 
like image 527
Echo Avatar asked Jul 17 '11 21:07

Echo


People also ask

Can we call superclass constructor?

We can Access SuperClass members using super keyword as constructor is not a part of class, so while calling it cannot be implemented, by using SUPER() we can call the members and memberfunctions in constructor.

What is super () in constructor?

The super keyword is used to call the constructor of its parent class to access the parent's properties and methods.

Is super constructor always called?

Super class constructor is always called during construction process and it's guaranteed that super class construction is finished before subclass constructor is called. This is the case for most if not all the object oriented language.


2 Answers

case class CustomException(errorMsg:String)  extends Exception(errorMsg) 
like image 75
Kim Stebel Avatar answered Oct 02 '22 05:10

Kim Stebel


case class CustomException(errorMsg:String)  extends Exception(errorMsg) 

You're calling the superclass's constructor, but the argument you are passing (error) isn't bound to anything.

like image 31
dhg Avatar answered Oct 02 '22 05:10

dhg