Skip to content

Commit cdbd348

Browse files
committed
Add Ruby snippet for defining and raising a custom error class
1 parent ec3ee2b commit cdbd348

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
title: Custom Error Class
3+
description: Defines and raises a custom error class in Ruby.
4+
author: ACR1209
5+
tags: ruby,error handling,custom error
6+
---
7+
8+
```rb
9+
class MyCustomError < StandardError; end
10+
11+
def risky_method(value)
12+
raise MyCustomError, "Value must be positive" if value <= 0
13+
"Valid value: #{value}"
14+
end
15+
16+
# Usage:
17+
begin
18+
puts risky_method(-1)
19+
rescue MyCustomError => e
20+
puts e.message # Output: "Value must be positive"
21+
end
22+
```

0 commit comments

Comments
 (0)