function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AniltAnilt 

Getting java.lang.ClassCastException error

Hi Everyone,

 

I want to display custom Exception in the page, so written code like ths

 

VF Code:

 

<apex:page controller="AgeException">
<apex:form >
<apex:pageMessages />
<apex:pageBlock >
<apex:inputText value="{!age}"/>
<apex:commandButton value="Submit" action="{!submit}"/>
</apex:pageBlock>
</apex:form>
</apex:page>

 

ApexCode:

 

Public class AgeException extends Exception
{
Public integer age {get; set;}
Public Pagereference submit()
{
If(age<20 || age>100)
{
throw new AgeException('Invalid Age: Age must be between 20 and 100');
}
}
}

 

Error:

 

java.lang.ClassCastException: common.apex.runtime.impl.ExecutionException cannot be cast to common.apex.runtime.bytecode.BytecodeApexObject

 

Please help me regarding this.

 

2nd question:

 

FYI....I've tried the same code with few modifications for adding the error message in the page and succeded

 

VF Code is same as above and Apex code as stated below

 

Public class ageexc
{
Public integer age {get; set;}
Public void submit()
{
If(age<20 || age>100)
{
ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.WARNING, 'Invalid Age: Age must be between 20 and 100'));
}
}
}

 

Please let me know why is it working for adding error message and why is not working for adding Exception.

 

Best Answer chosen by Admin (Salesforce Developers) 
AniltAnilt

At last I'm able to add the error message in the VF Page..here is the code

<apex:page controller="ageexc">
<apex:form >
<apex:pageMessages />
<apex:pageBlock >
<apex:inputText value="{!age}"/>
<apex:commandButton value="Submit" action="{!submit}"/>
</apex:pageBlock>
</apex:form>
</apex:page>

 

public class ageexc

{
Public integer age {get; set;}
Public void submit()
{
If(age<20 || age>100)
{
ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.WARNING, 'Invalid Age: Age must be between 20 and 100'));
}
}
}

All Answers

sfdcfoxsfdcfox

It works when I tried it. Note that you do not want to throw an exception. You'll get an error like this:

 

Visualforce Error
Help for this Page

customexception: Hah!
Error is in expression '{!throwexp}' in component <apex:page> in page customexception

The user wouldn't be given a chance to correct the error. Any exception must be caught before falling back to the Visualforce engine or the entire page will be rendered invalid.

 

Just politely return the error message so they can fix their input.

sfdcfoxsfdcfox

As a side note, I'd say you should submit your case to support.

AniltAnilt

At last I'm able to add the error message in the VF Page..here is the code

<apex:page controller="ageexc">
<apex:form >
<apex:pageMessages />
<apex:pageBlock >
<apex:inputText value="{!age}"/>
<apex:commandButton value="Submit" action="{!submit}"/>
</apex:pageBlock>
</apex:form>
</apex:page>

 

public class ageexc

{
Public integer age {get; set;}
Public void submit()
{
If(age<20 || age>100)
{
ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.WARNING, 'Invalid Age: Age must be between 20 and 100'));
}
}
}

This was selected as the best answer