初次使用try-catch遇到的问题

None.gif      class  UseException
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
private int smallNum;
InBlock.gif        
public UseException()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            smallNum
=0;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public UseException(int num)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(num>100)
InBlock.gif                
throw new ArgumentOutOfRangeException("输入值太大");
InBlock.gif            
else
InBlock.gif                smallNum
=num;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public int SmallNum
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return smallNum;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif    
class  Test
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                UseException example
=new UseException(10);
ExpandedSubBlockEnd.gif            }
    
InBlock.gif            
catch(ArgumentOutOfRangeException e)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(e.Message);
ExpandedSubBlockEnd.gif            }

InBlock.gif            Console.WriteLine(
"number={0}",example.SmallNum);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif


编译时出错:找不到类型或命名空间名称“example”(是否缺少 using 指令或程序集引用?)
但是如果将初始化语句改成UseException example=new UseException(1000);则可以抛出异常,去掉try-catch块后也可以运行。

我在CSDN上问了一下,得到的答复是
     “因为你的example是在try块里面定义的,出了try块就被释放掉,执行到
        Console.WriteLine("number={0}",example.SmallNum);
     就会说找不到这个对象。

     你初始值为1000,
        if(num>100)
        throw new ArgumentOutOfRangeException("输入值太大");
     就会被抛出一个异常,能够被捕获到。”
并且看到了个解决方法,将try-catch结构改成:

None.gif            UseException example = null ;
None.gif            
try
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif {
InBlock.gif                example
=new UseException(10);
ExpandedBlockEnd.gif            }
    
None.gif            
catch (ArgumentOutOfRangeException e)
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif {
InBlock.gif                Console.WriteLine(e.Message);
ExpandedBlockEnd.gif            }

None.gif            Console.WriteLine(
" number={0} " ,example.SmallNum);


null不能丢,否则一样报错。得到的解释是:
    “在一个方法里,通过一个引用调用一个对象的方法(或属性)之前,此引用必须先被定义或赋值”
我觉得try-catch在在方面与函数类似。比如

None.gif      class  c1
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
public static int i=0;
ExpandedBlockEnd.gif    }

None.gif    
class  Test
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
public static void fun()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            c1.i
+=6;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif

InBlock.gif        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            fun();
InBlock.gif            Console.WriteLine(
"i="+c1.i);
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

c1.i的值可以被Test.fun方法所影响。当然,如果把c1.i的static去掉,再在fun里重新初始化一个对象,就不会被改动
None.gif      class  c1
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
public int i=0;
ExpandedBlockEnd.gif    }

None.gif    
class  Test
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
public static void fun()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            c1 c
=new c1();
InBlock.gif            c.i
+=6;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            c1 c
=new c1();
InBlock.gif
InBlock.gif            fun();
InBlock.gif            Console.WriteLine(
"i="+c.i);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif

转载于:https://www.cnblogs.com/BlueskyGreenearth/archive/2005/03/25/125333.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值