• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Changing exception variables - why can't they be multi-letter?

RhinoCan

Well-Known Member
Jul 5, 2015
184
64
I'm developing my first Android apps in Android Studio 3.1.2.

I've noticed a strange quirk in Studio that has me puzzled. It's not a big deal but it is a bit annoying.

When I write a catch block for an exception, Studio only seems to allow for the variable name to be a single letter. For example, if I write:

Code:
try {
    //something
}
catch (NullPointerException excp) {
    excp.printStackTrace();
}

Studio highlights my variable name, excp in red and leaves it in red (in both places). If I change it back to 'excp' back to 'e', the error goes away and all is well.

Studio lets me have multi-letter variable names everywhere else so I'm puzzled by why this would be a problem in a catch block. Is there a good reason for this or have I stumbled on a bug?

I avoid single-letter variables if at all possible, even in the simplest code. It's a bit of a personal quirk, I suppose, and it's not a big problem if I have to use single letter variables on occasion but I'd just like to know if Studio is working the way it is supposed to? It seems an odd thing to force on people.
 
There's no restriction which limits you to one letter exception variable names. Just to double check, I pasted your code into my AS, and there was no problem.

Your problem is caused by something elsewhere in the code. Have you declared a duplicate variable?

Hover over the highlighted variable name, and it will tell you the what the specific error is.
 
Last edited by a moderator:
Upvote 0
There's no restriction which limits you to one letter exception variable names. Just to double check, I pasted your code into my AS, and there was no problem.

Your problem is caused by something elsewhere in the code. Have you declared a duplicate variable?

Hover over the highlighted variable name, and it will tell you the what the specific error is.

I'm glad to hear that single-letter variables aren't supposed to be a problem. I wish I had time to make a short video demonstrating the error I'm getting but I'm already running late and will be away for the day. I'll post again tomorrow. Maybe I *do* have a duplicate variable somewhere, although it seems unlikely since the scoping rules should ensure I can have lots of variables named 'excp' as long as they are in separate catch blocks....
 
Upvote 0
It turns out that I muddled some symptoms from the various problems I was mentioning on Friday. I just tried a catch block with a variable name of "excp" instead of "e" and Android Studio does NOT highlight both occurrences of "excp" in red: it puts a green squiggly underline under the *first* occurrence of "excp" - but not the second - and declares it a typo. Changing it back to "e" removes the claim that it is a typo but if I don't change the second occurrence back to "e" as well, the second occurrence will be red as would be expected because "e" is not the same as "excp".

My apologies for mis-describing the situation!

However, I'm puzzled about why "excp" is a typo but "e" is not. Any idea why that is the case? It seems to me that BOTH are typos in the sense of not being words in the English language. What do I do to persuade Studio that "excp" is NOT a typo and should be left unflagged? Do I need to add it to a dictionary used by Studio? How do I do that?
 
Upvote 0
File -> Settings (Project Settings [...]) -> Inspections -> Spelling -> Type

This brings up three options that can be enabled/disabled:
  1. Process code
  2. Process literals
  3. Process comments
You can disable all by deselecting the Typo check box

Thank you! Turning off "process code" persuaded it to leave my "excp" alone instead of declaring it a typo :)

FYI: Your directions for getting to the relevant settings is slightly off (at least in Android Studio 3.1.2). I clicked on File->Settings->EDITOR->Inspections. Then in the right hand pane, I found Spelling, clicked on the down arrow beside it, then saw the three options you mentioned further to the right.

https://stackoverflow.com/questions...l-check-on-field-names-in-android-studio?rq=1

At least I've solved one problem now, even if it was the one that was delaying me the least. Thanks again!
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones