0
I am working on the development and refactoring of some Activity's
that has similar behaviors in our projects, so to avoid code repetitions, for common functionalities, I created some Activity's
base, which are inherited by the other Activities’s.
My problem is this, as what I’m creating is class
groundwork (library), that can be used by several others, I need to ensure that whoever uses it will use it in the right way. Therefore, I would like to verify non-conformities in the parameters of class
base and launch a Exception
in case of non-conformities, so that these errors are captured in the development and testing phase.
That does not seem possible, follow what I would like to do next:
@Override
protected void onCreate(Bundle savedInstanceState) {
userHelper = new UserHelper(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.container_for_fragment);
if (savedInstanceState == null) {
if (mFragment != null) {
getFragmentManager().beginTransaction()
.add(R.id.container, mFragment).commit();
}else{
// essa linha não compila: "Unhandled exception type Exception"
throw new Exception("mFragment, deve ser setado antes da chamada do onCreate() da class base \"ListAbstractActivity\"");
}
}
}
Doubts
- Is there any way to do something about it?
- What I’m trying to do is right?
- Is there any plausible alternative to this situation?
OMG, @Piovezan, damn it I don’t use
Exceptions
for flow control, so I don’t have much knowledge about them. So there’s this "checked Exception". SoException
and their descendants, are for exceptions that I hope and want to treat them, andRuntimeException
and their descendants for exceptions that should really break the application. Right? Could briefly explain the difference between the two?– Fernando Leal
Just to complement, I implemented the way you mentioned and apparently works correctly. + 1, and if all goes well I already mark as answer. And thank you for the clarification.
– Fernando Leal
The explanation is basically how you spoke. A checked Exception needs to be treated or declared in the method signature. They are basically for "recoverable" situations, while the Runtime are for programming errors. But there is a little more to talk about exceptions that would make the explanation longer. See for example this question in the OS.
– Piovezan
So, because this is a somewhat broad subject, and outside the scope of this question, I’m going to check if there are already, and eventually create a new question here at Sopt.
– Fernando Leal
Postei this question regarding
Exception
vsRuntimeException
– Fernando Leal