写作例外 [英] writing exceptions

查看:76
本文介绍了写作例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. 如何做到这一点向全班添加新学生的方法?如果试图在班级中已经存在具有相同ID的学生,并且试图将学生添加到班级中,则应该抛出异常.

解决方案

您好timalamu,

那不是对异常的正确使用.如果两个名称相同,则您可能会使用一条消息告诉用户两个输入的名称相同.

异常仅表示字符外行为.请查看MSDN中的异常说明:AC#异常是对程序运行时出现的异常情况的一种响应,例如尝试除以零.


  1. how do I do this "Add a method to the class which adds a new student? It should throw an exception if an attempt is made to add a student to the class if a student with the same ID already exists in the class".

解决方案

Hi timalamu,

That is not the proper use of exception. If both name is same you could probably use a message to saying the user that both entered name is same.

Exception says only the out of character behavior. Please look into the exception defimation from MSDN: A C# exception is a response to an exceptional circumstance that arises while a program is running, such as an attempt to divide by zero.


这篇关于写作例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆