Nguyen  Minh Binh
Nguyen Minh Binh

Reputation: 24443

Is there any way to auto catch exception?

Suppose I have 10 lines of code. Any line maybe throw a NullPointerException. I don't want to care about these exceptions. If a line throws the exceptions, I want the executor jump to next line and go forward. Could I do this on Java? if yes, please give me some sample code.

UPDATE: (add sample source for more clear question)

first.setText(prize.first);
second.setText(prize.second);
third.setText(prize.third);
fourth.setText(prize.fourth);
fifth.setText(prize.fifth);
sixth.setText(prize.sixth);
seventh.setText(prize.seventh);
eighth.setText(prize.eighth);

Suppose I have 8 lines of code above. What I want is: if the line 4 (or 5, 6,...) throws an exception, all other lines of code works normally. Of course I can use try...catch to catch the exceptions line by line. But this way make my source very complex.

Upvotes: 1

Views: 1115

Answers (5)

Natix
Natix

Reputation: 14257

This is impossible. If you think about it for a while, you'll realize that that is also a nonsense. Take this code for example:

String s = getSomeString(); // assigns null
int length = s.length; // throws NPE

if (length > 10) { // what would you do here?? length is un-initialized
    domeSomething();
}

Edit

Now that you've updated you question with a code snippet, there are two general approaches to deal with nulls:

1. Disallow them where they aren't supposed to be

In your code snippet, you've provided a list of EditTexts, where any o them might be null. Well, in my opinion if you have an Activity in Android which should have some Views, then it is a programming error if any of them is null for whatever reason. An activity without some of its views can't work properly, so throwing a NPE and exiting the application is a perfectly sensible thing to do.

The NPE tells you "hey, you've made a mistake and you have to fix it". Until then, it is not safe for the app in an unpredicted state to continue execution, because no one knows what can happen, what data can become corrupted.

2. Allow them where they make sense, and provide null-checks

There are some places where you can have variables whose value can be empty. There are several approaches how to express an empty value and null is the most obvious one.

In that case, you can't just ignore the possibilty of a NPE and you have to do some null-checks. But again, a NPE is not your enemy. It will tell you that you've forgotten to account for all the possible situations that you should have.

Of course, repeated null checks make the code cluttered and noisy, so you can create some methods that generalize the null checks for you. I could not come up with a sensible example, so take this method only for illustration:

public void clearEditTexts(EditText... editTexts) {
    for (EditText e : editTexts) {
        if (e != null) {
            e.setText("");
        }
    }
}

This method clears the contents of all the passed EditTexts. Any of them can be null, but this method does these null-checks internally, so you don't have to do it manually for each of them.


This question is also a very good reading about handling nulls: Avoiding != null statements

Upvotes: 7

Chris Forrence
Chris Forrence

Reputation: 10104

The fact that a variable is null is notable, and should not be ignored. Therefore, you'd want something like this:

...
if(first != null && prize.first != null)
{
    first.setText(prize.first);
}
...

If, for some reason, you absolutely need to throw NPEs, you would need to surround each line with its own try/catch; there's no way to automatically catch exceptions and have it advance to the next line. It's not, however, recommended to do this.

...
try
{
    first.setText(prize.first);
}
catch(NullPointerException e)
{
}
try
{
    second.setText(prize.second);
}
catch(NullPointerException e)
{
}
...

Upvotes: 3

someone
someone

Reputation: 6572

NullPointerException is a runtime (unchecked) exception and Java not reccomand to catch unchecked exceptions. You should have a solution to prevent it.

Upvotes: 1

Boann
Boann

Reputation: 50041

This is not possible. If your variables can be null you will just have to check for that, or prevent them from being null. Very rarely it can be useful to create a dummy object implementation whose overridden methods do nothing, so it can be used in place of a valid object instead of null.

Upvotes: 6

MustSeeMelons
MustSeeMelons

Reputation: 756

No, you cant and you should care, they try to tell you whats wrong.

Upvotes: 3

Related Questions