Try & Catch Statement in After Effects

In this post, I will go over the Try & Catch Statements in After Effects. So without any further ado, let’s dive right in!

Alright, so we are in After Effects. For this example, we are going to pick up where we left off in tip number 37.

If you remember, I showed you how to take the first letters from the name Elijah Patrick, and place them inside of an orange circle in our composition like so:

Grabbing letters from a word and placing it inside of an orange circle in After Effects

We were doing this with the Split Method Expression:

nameText.split(” “)[0].charAt(0) + nameText.split(” “)[0].charAt(0)

Split Method in After Effects

But watch what happens if I change it from two words to just one word.

Expression breaks in one word example in After Effects

As you can see, the expression breaks. It stops working. So that is where a Try & Catch Statement comes in very handy.

I am going to type try{} statement and then I will add our Split Method inside of the curly brackets like so:

try {

nameText.split(” “)[0].charAt(0) + nameText.split(” “)[0].charAt(0)

}

If that expression breaks or gives us some kind of error, then we are going to catch that error with the catch(err){} statement and run another expression inside of those curly brackets like so:

catch(err) {

nameText.split(” “)[0].charAt(0)

}

The full expression should look like this:

try {

nameText.split(” “)[0].charAt(0) + nameText.split(” “)[0].charAt(0)

}

catch(err) {

nameText.split(” “)[0].charAt(0)

}

catch(err) expression in After Effects

In other words, it will try and execute this expression first:

nameText.split(” “)[0].charAt(0) + nameText.split(” “)[0].charAt(0)

and if it gives us an error, then it will go to this expression instead:

nameText.split(” “)[0].charAt(0)

Which means pulling just one character from one word instead of two.

try{} expression in After Effects

As you can see, it is working quite well. I can still type more than one word and it works quite well.

Typing more than one word in try and catch statement in After Effects

I hope you found this post useful. If you would like to learn more about Expression and how it can speed up your workflow in After Effects, check out my Learn After Effects Expressions Course.

About the author:

Sergei Prokhnevskiy

Sergei Prokhnevskiy is a Charlotte, NC based motion graphics designer, an online tutor, an entrepreneur, and Adobe MAX Master Speaker. Sergei is known for his quick and practical After Effects tips and tricks on YouTube, where he generated well over 6 million views on his tutorials. He has authored an online video course on After Effects Expressions and developed scripts for After Effects. He is also a former senior motion graphics designer at Fox Sports where he worked on graphics for NFL, Super Bowl, MLB, World Series, College Football, NASCAR, Women’s World Cup, and NASCAR Race Hub. He is very passionate about learning and teaching others.

5 FREE TIME-SAVING TOOLS

Over 100,000 downloads & counting!

These 5 After Effects tools will seriously speed up your workflow and save you hours of wasted time. More than that, they could be the difference between being able to complete a project on time or not!