IT-Academy

Osetrenie chyb

Nov 30th, 2017
241
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. Sub TestovaciaProcedura()
  3.    
  4.     Dim i As Integer
  5.    
  6.     Dim x As Integer
  7.    
  8.     For i = -1 To 1
  9.        
  10.         x = 3 / i
  11.        
  12.     Next i
  13.    
  14. End Sub
  15.  
  16.  
  17. Sub OsetrenieChyby1()
  18.    
  19.     Dim i As Integer
  20.     Dim x As Integer
  21.    
  22.     'pri chybe skoè na dalsi riadok
  23.    
  24.     On Error Resume Next
  25.    
  26.     For i = -1 To 1
  27.         x = 3 / i
  28.         MsgBox (x)
  29.     Next i
  30.    
  31. End Sub
  32.  
  33. Sub OsetreniChyby2()
  34.     Dim i As Integer
  35.     Dim x As Integer
  36.    
  37.     'pri chybe skoè na dalši riadok
  38.    
  39.     On Error Resume Next
  40.     For i = -1 To 1
  41.        
  42.         x = 3 / i
  43.        
  44.         'zaregistroval objekt Err chybu?
  45.        
  46.         If Err <> 0 Then
  47.            
  48.             'evidencia èísla a popisu chyby
  49.            
  50.             'do okna Immediate(anglicky)
  51.            
  52.             Debug.Print Err.Number & vbTab & Err.Description
  53.            
  54.             'reset
  55.            
  56.             Err.Clear
  57.            
  58.         End If
  59.        
  60.     Next i
  61.    
  62. End Sub
  63.  
  64. Sub OsetrenieChyby3()
  65.    
  66.     Dim i As Integer
  67.     Dim x As Integer
  68.    
  69.     'pri chybe skoè na riadok Chyba
  70.    On Error GoTo chyba
  71.    
  72.     For i = -1 To 1
  73.         x = 3 / i
  74.         MsgBox x
  75.     Next i
  76.    
  77. chyba:
  78.     MsgBox "Chyba: Delil si 0..."
  79. End Sub
  80.  
  81. Sub OsetrenieChyby4()
  82.    
  83.     Dim i As Integer
  84.     Dim x As Integer
  85.    
  86.     'pri chybe skoè na riadok Chyba
  87.    
  88.     On Error GoTo chyba
  89.    
  90.     For i = -1 To 1
  91.        
  92.         x = 3 / i
  93.        
  94.     Next i
  95.    
  96.     Exit Sub
  97.    
  98. chyba:
  99.    
  100.     'chyba delenia nulou
  101.    
  102.     If Err.Number = 11 Then MsgBox (Err.Description)
  103.    
  104.    
  105.    
  106.     'ošetrenie promennej
  107.    
  108.     i = i + 1
  109.    
  110.     'návrat na riadok, ktorý vyvolal chybu
  111.    
  112.     Resume
  113.    
  114.    
  115.    
  116.     'alternativa bez priameho ošetrenia promennej
  117.    
  118.     'Resume Next
  119.    
  120. End Sub
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment