# How to handle errors and prevent script from crashing after

**URL:** <https://discourse.julialang.org/t/how-to-handle-errors-and-prevent-script-from-crashing-after/138168>\
**Category:** General Usage\
**Tags:** question, error, control-flow\
**Created:** [July 14, 2026, 2:51am UTC](https://discourse.julialang.org/t/how-to-handle-errors-and-prevent-script-from-crashing-after/138168 "2026-07-14T02:51:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mervyn\_Larrier](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mervyn_larrier/32/37709_2.png) [@Mervyn\_Larrier](https://discourse.julialang.org/u/Mervyn_Larrier)\
**Post date:** [July 14, 2026, 2:51am UTC](https://discourse.julialang.org/t/how-to-handle-errors-and-prevent-script-from-crashing-after/138168/1 "2026-07-14T02:51:38Z")

</div>

Hi all. I’m writing a script to make a job a little bit easier, and it may be something I share with others so I’m trying to put a bit more care into making it. I’m not very familiar with error handling and I’m struggling with the control flow. How do I get the script to retry the failed action instead of quitting the process entirely? Most of the code presented works, except for the part where try/catch comes in. When those are touched, the script breaks.

```julia-auto
# radius_roll will be the approx. measured radius of the roll, measured before unrolling
# thickness_band will be the meausured thickness of the band in the direction of the groove, measured before unrolling; b in the archimedian spiral equation.
# tau represents the circle constant, where tau/2 = pi. Equal to one revolution of a point a point some distance from the origin
# n will be the number of coils on the roll
# length will be the calculated length of the roll
# radius_axle will be the radius of the hole in the center of the roll

#Things to consider: freshness of the roll, machine last used on, should probably spit out a short, dated report.

const tau = 2*π

trigger = false

# the math doesn't change, this is for redundancy.
println("Choose your units: inches (\") or centimeters (cm)\n")
ans = readline()
while trigger == false
	if ans == "\""
		println("Inches selected\n")
		global trigger = true
	elseif ans == "cm"
		println("Centimeters selected\n")
		global trigger = true
	else
		println("Please input a valid unit of measurement from the selection: inches (\") or centimeters (cm)\n")
		global ans = readline()
	end
end

println("What is the radius of the roll in $ans?\n")
try
	global radius_roll = parse(Float64, readline())
catch e0
	throw(error("Please input a valid number for the radius of the roll in $ans.\n"))
end

println("You input $radius_roll $ans. Is that correct? Y/N\n")
ans1 = uppercase(readline())

while ans1 != "Y"
	if ans1 == "Y" 
		break
	elseif ans1 == "N"
		println("What is the radius of the roll in $ans?\n")
		try 
			global radius_roll = parse(Float64, readline())
		catch e1
			throw(error("Please input a valid number for the radius of the roll in $ans.\n"))
		end
		println("You input $radius_roll $ans. Is that correct? Y/N\n")
		global ans1 = uppercase(readline())
	elseif ans1 != "Y" && ans1 != "N"
		println("Please input a valid answer: Y/N\n")
		global ans1 = uppercase(readline())
	end
end

```

---

<div class="post-metadata">

**Author:** ![penelopeysm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/penelopeysm/32/213172_2.png) [@penelopeysm](https://discourse.julialang.org/u/penelopeysm)\
**Post date:** [July 14, 2026, 3:27am UTC](https://discourse.julialang.org/t/how-to-handle-errors-and-prevent-script-from-crashing-after/138168/2 "2026-07-14T03:27:16Z")

</div>

The easiest way is to use [`tryparse`](https://docs.julialang.org/en/v1/base/numbers/#Base.tryparse) instead, which returns `nothing` instead of throwing an error, meaning that you can use normal control flow like `while` instead of `try`/`catch`:

```julia
radius_roll = nothing
while radius_roll === nothing
    println("What is the radius of the roll in $ans?\n")
    radius_roll = tryparse(Float64, readline())
    if radius_roll === nothing
        println("Please input a valid number")
    end
end

```

There’s actually a bug here: if the input stream is closed, `readline()` will keep on returning nothing, and the loop will go forever. You can trigger this by pressing Ctrl-D at the terminal when prompted for the radius.

To fix this, check for this with [`eof`](https://docs.julialang.org/en/v1/base/io-network/#Base.eof):

```julia
radius_roll = nothing
while radius_roll === nothing
    println("What is the radius of the roll in $ans?\n")
    radius_roll = tryparse(Float64, readline())
    if eof(stdin)
        error("no radius given")
        # or do whatever you want
    end
    if radius_roll === nothing
        println("Please input a valid number")
    end
end

```

You can technically make it work with the original `parse` by wrapping it in a function and recursively calling itself:

```julia
function get_number()
    println("I can has number?")
    return try
        parse(Float64, readline())
    catch e
        eof(stdin) && rethrow()
        get_number()
    end
end
get_number()

```

---

<div class="post-metadata">

**Author:** ![Mervyn\_Larrier](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mervyn_larrier/32/37709_2.png) [@Mervyn\_Larrier](https://discourse.julialang.org/u/Mervyn_Larrier)\
**Post date:** [July 14, 2026, 4:48am UTC](https://discourse.julialang.org/t/how-to-handle-errors-and-prevent-script-from-crashing-after/138168/3 "2026-07-14T04:48:33Z")

</div>

That looks very simple, I like it. Is it better generally to try and reduce the problem to more simple control flow like this? When would try/catch be more appropriate?

---

<div class="post-metadata">

**Author:** ![Mervyn\_Larrier](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mervyn_larrier/32/37709_2.png) [@Mervyn\_Larrier](https://discourse.julialang.org/u/Mervyn_Larrier)\
**Post date:** [July 14, 2026, 5:33am UTC](https://discourse.julialang.org/t/how-to-handle-errors-and-prevent-script-from-crashing-after/138168/4 "2026-07-14T05:33:01Z")

</div>

It also occurs to me to ask: is it better code hygiene to wrap this sort of thing in a function? It seems like it might be, and I was assuming a function would be overkill for the task.

---

<div class="post-metadata">

**Author:** ![NathanEvans](https://avatars.discourse-cdn.com/v4/letter/n/91b2a8/32.png) [@NathanEvans](https://discourse.julialang.org/u/NathanEvans)\
**Post date:** [July 14, 2026, 8:18am UTC](https://discourse.julialang.org/t/how-to-handle-errors-and-prevent-script-from-crashing-after/138168/5 "2026-07-14T08:18:45Z")

</div>

Using a `while` loop around the `try` block is usually the simplest approach. That way, if an error happens, you catch it, show a message, and continue the loop instead of exiting the script. Once the operation succeeds, you can `break` out of the loop. Keeping the retry logic outside the `try` block also makes the control flow much easier to follow.

---

<div class="post-metadata">

**Author:** ![penelopeysm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/penelopeysm/32/213172_2.png) [@penelopeysm](https://discourse.julialang.org/u/penelopeysm)\
**Post date:** [July 14, 2026, 10:32am UTC](https://discourse.julialang.org/t/how-to-handle-errors-and-prevent-script-from-crashing-after/138168/6 "2026-07-14T10:32:09Z")

</div>

For a small script like yours I would say it doesn’t really matter, but functions generally serve an important purpose of organising code, so the moment you have nontrivial stuff, definitely!
