首页 > 其他分享 >SIT232面向对象开发

SIT232面向对象开发

时间:2023-05-06 15:24:42浏览次数:32  
标签:exception system SIT232 will 面向对象 开发 https exceptions your

SIT232程序辅导、辅导Python/Java程序
SIT232 Object-Oriented Development Trimester 1, 2023
1
Practical Task 4.1
(Pass Task)
Submission deadline: Monday, April 17
Discussion deadline: Saturday, May 6
General Instructions
This practical task introduces you to error handling and covers several most common .NET Framework
exception types that you will likely encounter as soon as you start developing larger applications. Exceptions
that you will meet in practice can be thrown either by the runtime or the application’s code. The latter case
represents a class of exceptions that are defined by the application’s developer. Some exceptions can be
caused by a faulty program code, while other exceptions can result from an incorrect input that has not been
accounted for in the application's code. When any of these errors occur, the system catches the error and
raises an exception.
The process of generating and signalling an error is referred to as throwing an exception. This is done using
the ‘throw’ keyword followed by a new instance of a class derived (inherited) from System.Exception. There
are standard exception types provided by the .NET framework that you can use rather than creating a custom
exception.
Remember that execution failure occurs whenever a program (or its part) cannot do what it was designed to
do. For example, if the OpenFile method cannot return a file handler to the caller, it would be considered an
execution failure. Exceptions are the primary means of reporting errors in applications. Therefore, you must
adhere to a number of rules when you throw or handle exceptions. Here are several basic rules that you
should follow and always think about them when you work on your application.
Report execution failures by throwing exceptions.
Do not use exceptions for the normal flow of control, if possible.
Consider terminating the program by calling System.Environment.FailFast method instead of
throwing an exception if your code encounters a situation where it is unsafe for further execution.
Consider the performance implications of throwing exceptions. Throw rates above 100 per second are
likely to noticeably impact the performance of most applications.
Do not have public members that can either throw or not based on some option.
Do not have public members that return exceptions as the return value.
Do not throw exceptions from exception filter blocks (i.e. the ‘catch’ block of the ‘try-catch’ statement).
Avoid explicitly throwing exceptions from ‘finally’ blocks.
This list is by no means exhaustive and there are more rules and strategies. Remember that exception
handling is an art which once you master grants you immense powers.
1. Start this practical task with watching lecture 3. In this task, you will need to conduct a small research
about every exception presented in the following list:
a) NullReferenceException
b) IndexOutOfRangeException
c) StackOverflowException
d) OutOfMemoryException
e) DivideByZeroException
f) ArgumentNullException
g) ArgumentOutOfRangeException
h) FormatException
i) ArgumentException
j) SystemException
In Section 9 of SIT232 Workbook, you will find a general information on how to catch and handle
exceptions along with some details on the above exceptions. These exceptions are also briefly described
and exemplified in the lecture notes for week 3 (you though are not allowed to copy the examples from
the lecture into your solution). However, you will still need to do search in the Internet to gather more
details about their usage. We also recommend you to refer to the .NET Framework reference
SIT232 Object-Oriented Development Trimester 1, 2023
2
documentation, which is rich of various examples. Finally, do not forget to look at the references at the
bottom of this task.
The expected solution for this task must consist of two parts: a program code that creates a situation
that generates each of the above exceptions and a text report that explains in your own words the facts
you found. As the result of your study, your report must answer the following questions and discuss the
related issues for each of the above exceptions.
What is a possible situation that leads to the exception?
Who is in charge of throwing the exception: you as a programmer or the runtime system? In theory,
should you throw exceptions of this type? Explain your answer.
If you need to throw the exception, what details would you provide as a message to the user (caller)?
What parameters would you include in the message?
Can the exception be generally caught (and therefore handled)?
If the exception occurs, should you generally catch this exception type or is it better to pass such
exception to the user (caller)? It is not enough to say yes or no; you must provide an argument to
support your answer.
Is the exception a case when you want to avoid this exception to occur in your application in general?
If so, what would be your actions as a programmer to avoid it?
Note that you must check whether all the above questions are actually relevant to a particular exception
that you consider. Remember that some exceptions are reserved and are to be thrown only by the
runtime system; in most cases they need to indicate a serious runtime problem. Other exceptions can be
programmer-defined and report about an incorrect input data or anticipated application failures. Thus,
some exceptions you can avoid by doing a proper argument checking. So, to answer these questions
correctly, you will have to focus on multiple implementation aspects and potential situations associated
with each exception type.
2. The following is an illustrative example of how your solution may look like. Here, we discuss the
InvalidOperationException type and give a sample code that throws it. This exception type is thrown
when a method call is invalid for the object's current state. This, for example, can be caused by changing
a collection (e.g., the content of an instance of the List<T>) while iterating it (e.g., via the ‘foreach’
statement). Programmers can also throw this exception type in cases where they want to signal to their
applications that the requested operation through a method call cannot be performed because the object
is in an invalid state. (Note that you may meet this exception in the credit Task 4.2 soon.)
Consider the following program code:
class Account
{
public string FirstName { get; private set; }
public string LastName { get; private set; }
public int Balance { get; private set; }
public Account(string firstName, string lastName, int balance)
{
FirstName = firstName;
LastName = lastName;
Balance = balance;
}
public void Withdraw( int amount )
{
if ( amount > Balance )
{
throw new InvalidOperationException("Insufficient fund");
}
Balance = Balance ‐ amount;
}
}
SIT232 Object-Oriented Development Trimester 1, 2023
3
The code above shows an Account class with properties to hold the account owner's names and account
balance. The Withdraw method deducts the withdrawal amount from the balance, and if the withdrawal
amount is higher than the balance, it throws an exception — the InvalidOperationException. When
the exception is raised, it cancels the withdraw transaction and signals to the application that the account
cannot be debited of that amount because of insufficient funds.
Thus, the InvalidOperationException exception is thrown in cases where the failure to invoke a
method is caused by reasons other than invalid arguments. Because the InvalidOperationException
exception can be thrown in a wide variety of circumstances, it is important to read the exception message
returned by the Message property. How you handle the exception depends on the specific situation. Most
commonly, however, the exception results from your own or user’s incorrect actions, and therefore it can
be anticipated and avoided.
3. Create a new C# Console Application project and import (replacing the existing file) the Program.cs file
attached to this task. The main Program class, and specifically its Main method, should sequentially
address every exception from the list via a block of program code (if applicable) placed inside the ‘try’
section of the respective ‘try-catch’ statement. For instance, our example given for the
InvalidOperationException exception type can be written as follows:
This code prints the following message to the terminal:
The following error detected: System.InvalidOperationException with message "Insufficient fund"
Note that the ‘try’ section of the block must contain code that causes the error expected in the ‘catch’
section. In our case, the account.Withdraw(1000) will throw the InvalidOperationException
exception, which then will be handled in the ‘catch’ section. Furthermore, if you need to add an auxiliary
class (e.g. the Account class in our example), then place it inside the Program.cs file next to the existing
Program class. For your convenience, we provide a template of Program.cs containing our example.
Note that for some exceptions you do not need to provide a program code but your text answer in the
report. We do not specify here the particular exception from the list because we expect you to give a
clear reason for omitting it.
4. We expect you to answer the questions in the text report and write the associated code examples in the
Program.cs file. Every exception must be discussed in no more than half a page of text. Your solution
should be concise and answer the relevant questions exactly. Remember that you must explain solutions
in your words; that is, copying blocks of text from the Internet is never acceptable. Though you are
allowed to paraphrase answers found in the Internet, be prepared to explain them during the one-onone interview with your tutor.
Further Notes
Explore Section 9 of the SIT232 Workbook to learn how exceptions interrupt the execution of code and
can result in program termination; how to throw, catch and handle exceptions; what exception classes
are provided by Microsoft .NET and how to define your own classes to form an exception hierarchy; and
when and how to apply exceptions in your programs. The Workbook is available in CloudDeakin
Resources.
try
{
Account account = new Account("Sergey", "P.", 100);
account.Withdraw(1000);
}
catch (InvalidOperationException exception)
{
Console.WriteLine("The following error detected: " +
exception.GetType().ToString()+ " with message \"" +
exception.Message+"\"");
}
SIT232 Object-Oriented Development Trimester 1, 2023
4
 The following links extracted from the .NET Framework reference documentation will give you more
insights about error handling and some standard .NET exception types that you need to cover in the
report:
· https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/exceptions
· https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/exceptions
· https://docs.microsoft.com/en-us/dotnet/api/system.formatexception
· https://docs.microsoft.com/en-us/dotnet/api/system.dividebyzeroexception
· https://docs.microsoft.com/en-us/dotnet/api/system.overflowexception
· https://docs.microsoft.com/en-us/dotnet/api/system.argumentnullexception
· https://docs.microsoft.com/en-us/dotnet/api/system.argumentoutofrangeexception
· https://docs.microsoft.com/en-us/dotnet/api/system.systemexception
· https://docs.microsoft.com/en-us/dotnet/api/system.nullreferenceexception
· https://docs.microsoft.com/en-us/dotnet/api/system.indexoutofrangeexception
· https://docs.microsoft.com/en-us/dotnet/api/system.stackoverflowexception
· https://docs.microsoft.com/en-us/dotnet/api/system.outofmemoryexception
· https://learn.microsoft.com/en-us/dotnet/api/system.argumentexception?view=net-7.0
There are many other useful resources in the Internet that thoroughly cover this topic, for example:
· https://www.dotnetperls.com/exception
· https://www.c-sharpcorner.com/article/exception-handling-in-C-Sharp/
· https://www.geeksforgeeks.org/exception-handling-in-c-sharp/
Submission Instructions and Marking Process
To get your task completed, you must finish the following steps strictly on time.
Make sure your programs implement the required functionality. They must compile and have no runtime errors.
Programs causing compilation or runtime errors will not be accepted as a solution. You need to test your programs
thoroughly before submission. Think about potential errors where your programs might fail.
Submit the expected code files as a solution to the task via OnTrack submission system.
Once your solution is accepted by your tutor, you will be invited to continue its discussion and answer relevant
theoretical questions through a face-to-face interview. Specifically, you will need to meet with the tutor to
demonstrate and discuss the solution in one of the dedicated practical sessions (run online via MS Teams for cloud
students and on-campus for students who selected to join classes at Burwood). Please, come prepared so that the
class time is used efficiently and fairly for all students in it. Be on time with respect to the specified discussion
deadline.
You will also need to answer all additional questions that your tutor may ask you. Questions will cover the lecture
notes; so, attending (or watching) the lectures should help you with this compulsory discussion part. You should
start the discussion as soon as possible as if your answers are wrong, you may have to pass another round, still
before the deadline. Use available attempts properly.
Note that we will not accept your solution after the submission deadline and will not discuss it after the discussion
deadline. If you fail one of the deadlines, you fail the task, and this reduces the chance to pass the unit. Unless extended
for all students, the deadlines are strict to guarantee smooth and on-time work throughout the unit.
Remember that this is your responsibility to keep track of your progress in the unit that includes checking which tasks
have been marked as completed in the OnTrack system by your marking tutor, and which are still to be finalised. When
marking you at the end of the unit, we will solely rely on the records of the OnTrack system and feedback provided by
your tutor about your overall progress and the quality of your solutions.

 WX:codehelp

标签:exception,system,SIT232,will,面向对象,开发,https,exceptions,your
From: https://www.cnblogs.com/tongu1/p/17377439.html

相关文章

  • 2023全栈开发人员职业路线图
    0.全栈开发人员职业路线图全栈开发人员是IT行业中薪资最高的职业之一。如果您想成为一名全栈开发人员,以下是2023年全栈开发人员路线图上的十一个步骤:掌握敏捷开发和Scrum学习浏览器技术,如HTML和CSS熟练掌握JavaScript或TypeScript了解Git及其CI/CD生态系统具备移动应用......
  • 聚能量赢未来,OpenHarmony开发者大会开发工具分论坛圆满落幕
    4月19日,以“开源正当时,共赢新未来”为主题的开放原子开源基金会OpenHarmony开发者大会2023(以下简称“大会”)在北京举行,“开发工具分论坛”于当天下午召开。在本次论坛上,各位演讲嘉宾重点分享了OpenAtomOpenHarmony(以下简称“OpenHarmony”)3.2Release版本的开发工具、开发方法......
  • vue-移动端开发-样式适配方案
    样式适配场景:一个项目中既包含了web端也包含了移动端的页面,web端的一些样式不能在移动端正常显示解决方式:npmipostcss-px-to-viewport项目目录下postcss.config.jsmodule.exports={plugins:{"postcss-px-to-viewport":{unitToConvert:"px",view......
  • 嵌入式开发入门-51单片机基础知识(9)- DS18B20
    一、DS18B20简介1、DS18B20是一个数字温度传感器,单总线传输数据,测量温度范围为-45℃-125℃,测量精度可配置为9位,10位,11位,12位,默认配置12位,分别对应0.5℃、0.25℃、0.125℃和0.0625℃。二、温度测量1、由于DS18B20通电后,处于空闲状态,不进行温度测量和转换,所以必须发出转......
  • Android开发:使用Glide动态加载圆形图片和圆角图片
    最新消息,鼎鼎大名的Yelp应用也转投Glide的阵营了,而且Glide在跟Listview的配合起来非常的顺畅,Glide除了配置简单,还可以本地缓存图片,也可以实现Listview图片的提前预加载,使得listview的更加的顺滑,具体可以查看Yelp的那篇博文。但是如果碰到要把加载下来的图片转成圆角或者圆形的图......
  • Android开发中的一个小功能 清空搜索框的文字
    需求:项目中的有关搜索的地方,加上清空文字的功能,目的是为了增加用户体验,使用户删除文本更加快捷解决过程:开始的时候感觉这个东西不太好实现,主要就是布局的问题,可能是开始顾虑的太多了,再加上当时产品催的不太紧,而且这个功能也不是必须实现的。但是今天不一样了,这个是老大让加上的,说别......
  • 8-在集成开发环境当中开发Servlet程序
    1.集成开发工具很多,其中目前使用比较多的是:IntelliJIDEA(这个居多,IDEA在提示功能方面要强于Eclipse,也就是说IDEA使用起来比Eclipse更加智能,更好用。JetBrain公司开发的。收费的。)Eclipse(这个少一些),Eclipse目前还是有团队使用,只不过处于减少的趋势,自己从事工作之后,可能......
  • OrchardCore 中的 插件开发/ Shape / DisplayDriver / 视图扩展 / Razor代码注入
    请注意该文章仅限于OrchardCore项目中的DisplayDriver扩展机制,ASP.NETCOREMVC自身并没有对应功能,如果需要可以将相关的OrchardCore模块添加到项目中也可以实现响应功能背景最近一个功能需求,需要使用其它用户模拟身份,所以计划在用户列表页面扩展按钮组功能那么开始看代......
  • iOS开发系列--Swift语言
    概述Swift是苹果2014年推出的全新的编程语言,它继承了C语言、ObjC的特性,且克服了C语言的兼容性问题。Swift发展过程中不仅保留了ObjC很多语法特性,它也借鉴了多种现代化语言的特点,在其中你可以看到C#、Java、Javascript、Python等多种语言的影子。同时在2015年的WWDC上苹果还宣布Sw......
  • iOS开发系列--Swift进阶
    概述上一篇文章《iOS开发系列--Swift语言》中对Swift的语法特点以及它和C、ObjC等其他语言的用法区别进行了介绍。当然,这只是Swift的入门基础,但是仅仅了解这些对于使用Swift进行iOS开发还是不够的。在这篇文章中将继续介绍一些Swift开发中一些不常关注但是又必备的知识点,以便对Sw......