Any ideas how to prevent error when trying dynamically access label which doesn't exist? Check somehow whether it exists before referencing or maybe catch this error.

Here is my page:

    <apex:page Controller="MyController">
        <apex:outputText> {!$Label[label.labelName]} </apex:outputText>

And here is error:

Field $Label.myLabel does not exist. Check spelling
Error is in expression '{!$Label[label.labelName]}' in component <apex:repeat> in page pagename
Error evaluating dynamic reference 'myLabel'

Related topic: possible to access custom labels dynamically?

  • Why use a dynamic reference in the first place?
    – Adrian Larson
    Commented Oct 23, 2014 at 23:08
  • If the name of the label is something you solicit from the user, how about validating that at the time it is captured? Then if the label is deleted later, it's on their own head. Commented Jan 4, 2015 at 12:47
  • I tried Varun's APEX workaround in the question that was referenced by you. Unfortunately that doesn't work either, because when you input an invalid label via APEX, it throws an "Internal Salesforce Error" which cannot be caught. Commented Sep 30, 2016 at 12:05

1 Answer 1


Well, since you are already using labels as fields in the controller, why not simple render their value with a standard binding?

E.g. controller:

public class MyController
    public String mylabel1 {get; set;}
    public String mylabel2 {get; set;}

    public MyController()
         mylabel1='label one';   
         mylabel2='label two';   

VF page:

<apex:page Controller="MyController">
    <span> {!mylabel1} </span>
    <span> {!mylabel2} </span>

Should you by any chance try to add a binding referencing a field which does not exist in the controller (e.g. mylabel3), you won't be able to save your changes as you will get Save error 'Unknown property'.

  • 4
    This won't support all the features of labels, like overrides for managed packages, or language-specific translations that are automatically used based on use locale. Commented Mar 17, 2014 at 18:03
  • You can reference Labels in Apex as well. String foo = Label.myLabel;
    – ScottW
    Commented Sep 16, 2014 at 19:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .