Launch Exception on the onCreate of an Activity

Asked

Viewed 104 times

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

  1. Is there any way to do something about it?
  2. What I’m trying to do is right?
  3. Is there any plausible alternative to this situation?

1 answer

1


In case of error "Unhandled Exception type Exception" that you mentioned, this code will not compile because Exception is a checked Exception, that is, the method by which the spear is obliged to declare that it can launch it:

onCreate(...) throws Exception {

Since the compiler will probably not let you declare it (as it would change the method signature onCreate), launch instead a RuntimeException or one of your subclasses that you consider most appropriate.

  • 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". So Exception and their descendants, are for exceptions that I hope and want to treat them, and RuntimeException and their descendants for exceptions that should really break the application. Right? Could briefly explain the difference between the two?

  • 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.

  • 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.

  • 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.

  • Postei this question regarding Exception vs RuntimeException

Browser other questions tagged

You are not signed in. Login or sign up in order to post.