Skip to content
This repository

Subclass StandardError instead of Exception. #8

Merged
merged 1 commit into from about 2 years ago

2 participants

Scott Olson Chris Wailes
Scott Olson
tsion commented March 16, 2012

RLTK should use subclasses of StandardError instead of Exception. Exception is generally reserved for things which are fatal to Ruby itself and most application errors are under StandardError. Plus, the standard rescue without any argument catches any subclasses of StandardError, but not of Exception.

Chris Wailes chriswailes merged commit bccab55 into from March 18, 2012
Chris Wailes chriswailes closed this March 18, 2012
Chris Wailes
Owner

Thanks for the information. Given this new knowledge I would say your patch is about 90% correct. The InternalParserError and ParserConstructionError classes should really be subclasses of Exception, and as such renamed to indicate this. I've merged your repo and made the necessary changes. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Mar 16, 2012
Scott Olson Subclass StandardError instead of Exception. 084ecae
This page is out of date. Refresh to see the latest.
4  README
@@ -271,8 +271,8 @@ If the input (including the +ERROR+ token) can be reduced immediately the associ
271 271
 
272 272
 The example below for the unit tests shows a very basic usage of error productions:
273 273
 
274  
-	class AfterPlsError < Exception; end
275  
-	class AfterSubError < Exception; end
  274
+	class AfterPlsError < StandardError; end
  275
+	class AfterSubError < StandardError; end
276 276
 
277 277
 	class ErrorCalc < RLTK::Parser
278 278
 		production(:e) do
16  lib/rltk/ast.rb
@@ -6,7 +6,7 @@
6 6
 module RLTK # :nodoc:
7 7
 	# A TypeMismatch is thrown when an object being set as a child or value of
8 8
 	# an ASTNode is of the wrong type.
9  
-	class TypeMismatch < Exception
  9
+	class TypeMismatch < StandardError
10 10
 		
11 11
 		# Instantiates a new TypeMismatch object.  The first argument is the
12 12
 		# expected type and the second argument is the actual type of the
@@ -62,13 +62,13 @@ def self.child(name, type)
62 62
 						t = type
63 63
 						
64 64
 					else
65  
-						raise Exception, 'Child and Value types must be a class name or an array with a single class name element.'
  65
+						raise 'Child and Value types must be a class name or an array with a single class name element.'
66 66
 					end
67 67
 					
68 68
 					# Check to make sure that type is a subclass of
69 69
 					# ASTNode.
70 70
 					if not RLTK::subclass_of?(t, ASTNode)
71  
-						raise Exception, "A child's type specification must be a subclass of ASTNode."
  71
+						raise "A child's type specification must be a subclass of ASTNode."
72 72
 					end
73 73
 					
74 74
 					@child_names << name
@@ -152,13 +152,13 @@ def self.value(name, type)
152 152
 						t = type
153 153
 						
154 154
 					else
155  
-						raise Exception, 'Child and Value types must be a class name or an array with a single class name element.'
  155
+						raise 'Child and Value types must be a class name or an array with a single class name element.'
156 156
 					end
157 157
 					
158 158
 					# Check to make sure that type is NOT a subclass of
159 159
 					# ASTNode.
160 160
 					if RLTK::subclass_of?(t, ASTNode)
161  
-						raise Exception, "A value's type specification must NOT be a subclass of ASTNode."
  161
+						raise "A value's type specification must NOT be a subclass of ASTNode."
162 162
 					end
163 163
 					
164 164
 					@value_names << name
@@ -201,7 +201,7 @@ def children
201 201
 		# Assigns an array of AST nodes as the children of this node.
202 202
 		def children=(children)
203 203
 			if children.length != self.class.child_names.length
204  
-				raise Exception, 'Wrong number of children specified.'
  204
+				raise 'Wrong number of children specified.'
205 205
 			end
206 206
 			
207 207
 			self.class.child_names.each_with_index do |name, i|
@@ -247,7 +247,7 @@ def has_note?(key)
247 247
 		# the order they were declared).
248 248
 		def initialize(*objects)
249 249
 			if self.class == RLTK::ASTNode
250  
-				raise Exception, 'Attempting to instantiate the RLTK::ASTNode class.'
  250
+				raise 'Attempting to instantiate the RLTK::ASTNode class.'
251 251
 			else
252 252
 				@notes	= Hash.new()
253 253
 				@parent	= nil
@@ -277,7 +277,7 @@ def values
277 277
 		# Assigns an array of objects as the values of this node.
278 278
 		def values=(values)
279 279
 			if values.length != self.class.value_names.length
280  
-				raise Exception, 'Wrong number of values specified.'
  280
+				raise 'Wrong number of values specified.'
281 281
 			end
282 282
 			
283 283
 			self.class.value_names.each_with_index do |name, i|
2  lib/rltk/cfg.rb
@@ -21,7 +21,7 @@ module RLTK # :nodoc:
21 21
 	
22 22
 	# An exception class that represents a problem with a context-free
23 23
 	# grammar's definition.
24  
-	class GrammarError < Exception; end
  24
+	class GrammarError < StandardError; end
25 25
 	
26 26
 	# The CFG class is used to represent context-free grammars.  It is used by
27 27
 	# the RLTK::Parser class to represent the parser's grammar, but can also be
2  lib/rltk/lexer.rb
@@ -21,7 +21,7 @@ module RLTK # :nodoc:
21 21
 	
22 22
 	# A LexingError exception is raised when an input stream contains a
23 23
 	# substring that isn't matched by any of a lexer's rules.
24  
-	class LexingError < Exception
  24
+	class LexingError < StandardError
25 25
 		def initialize(stream_offset, line_number, line_offset, remainder)
26 26
 			@stream_offset	= stream_offset
27 27
 			@line_number	= line_number
10  lib/rltk/parser.rb
@@ -18,7 +18,7 @@ module RLTK # :nodoc:
18 18
 	
19 19
 	# A BadToken exception indicates that a token was observed in the input
20 20
 	# stream that wasn't used in the grammar's definition.
21  
-	class BadToken < Exception
  21
+	class BadToken < StandardError
22 22
 		def to_s
23 23
 			'Unexpected token.  Token not present in grammar definition.'
24 24
 		end
@@ -27,7 +27,7 @@ def to_s
27 27
 	# A NotInLanguage exception is raised whenever there is no valid parse tree
28 28
 	# for a given token stream.  In other words, the input string is not in the
29 29
 	# defined language.
30  
-	class NotInLanguage < Exception
  30
+	class NotInLanguage < StandardError
31 31
 		def to_s
32 32
 			'String not in language.'
33 33
 		end
@@ -35,7 +35,7 @@ def to_s
35 35
 	
36 36
 	# An exception of this type is raised when the parser encountered a error
37 37
 	# that was handled by an error production.
38  
-	class HandledError < Exception
  38
+	class HandledError < StandardError
39 39
 		
40 40
 		# The errors as reported by the parser.
41 41
 		attr_reader :errors
@@ -51,11 +51,11 @@ def initialize(errors, result)
51 51
 	end
52 52
 	
53 53
 	# Used for errors that occure during parser construction.
54  
-	class ParserConstructionError < Exception; end
  54
+	class ParserConstructionError < StandardError; end
55 55
 	
56 56
 	# Used for runtime errors that are the parsers fault.  These should never
57 57
 	# be observed in the wild.
58  
-	class InternalParserError < Exception; end
  58
+	class InternalParserError < StandardError; end
59 59
 	
60 60
 	# The Parser class may be sub-classed to produce new parsers.  These
61 61
 	# parsers have a lot of features, and are described in the main
4  test/tc_ast.rb
@@ -63,8 +63,8 @@ def test_equal
63 63
 	end
64 64
 	
65 65
 	def test_initialize
66  
-		assert_raise(Exception) { RLTK::ASTNode.new }
67  
-		assert_nothing_raised(Exception) { ANode.new(nil, nil) }
  66
+		assert_raise(RuntimeError) { RLTK::ASTNode.new }
  67
+		assert_nothing_raised(RuntimeError) { ANode.new(nil, nil) }
68 68
 	end
69 69
 	
70 70
 	def test_notes
4  test/tc_parser.rb
@@ -76,8 +76,8 @@ class ArrayCalc < RLTK::Parser
76 76
 	finalize
77 77
 end
78 78
 
79  
-class DummyError1 < Exception; end
80  
-class DummyError2 < Exception; end
  79
+class DummyError1 < StandardError; end
  80
+class DummyError2 < StandardError; end
81 81
 
82 82
 class ErrorCalc < RLTK::Parser
83 83
 	production(:e) do
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.