Pronto per verifica delle eccezioni (non so quali vengano sollevate)
This commit is contained in:
parent
2c4f0d2826
commit
0dc3eed65f
137
Program.cs
137
Program.cs
@ -1,9 +1,136 @@
|
|||||||
namespace eccezioni_1;
|
namespace eccezioni_1;
|
||||||
|
|
||||||
class Program
|
class Program {
|
||||||
{
|
static void Main(string[] args) {
|
||||||
static void Main(string[] args)
|
int scelta = 0;
|
||||||
{
|
double input = 0;
|
||||||
Console.WriteLine("Hello, World!");
|
bool eccezione;
|
||||||
|
int[] array = null;
|
||||||
|
do {
|
||||||
|
|
||||||
|
|
||||||
|
Console.WriteLine("Scegliere un'opzione:");
|
||||||
|
Console.WriteLine("1. NullPointerException array");
|
||||||
|
Console.WriteLine("2. NullPointerExceptiopn stringa");
|
||||||
|
Console.WriteLine("3. DivideByZeroException");
|
||||||
|
Console.WriteLine("4. IndexOutOfBoundException");
|
||||||
|
Console.WriteLine("0. Esci");
|
||||||
|
Console.Write("Scelta: ");
|
||||||
|
|
||||||
|
eccezione = false;
|
||||||
|
try {
|
||||||
|
scelta = Convert.ToInt32(Console.ReadLine());
|
||||||
|
}
|
||||||
|
catch (FormatException) {
|
||||||
|
Console.WriteLine("Opzione non valida.");
|
||||||
|
Pausa();
|
||||||
|
eccezione = true;
|
||||||
|
}
|
||||||
|
if (!eccezione) {
|
||||||
|
switch (scelta) {
|
||||||
|
case 0:
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
//try {
|
||||||
|
int[] T = null;
|
||||||
|
T[0] = 7;
|
||||||
|
//}
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
//try{
|
||||||
|
String s = null;
|
||||||
|
int l = s.Length;
|
||||||
|
//}
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
do {
|
||||||
|
Console.Write("Inserire un numero: ");
|
||||||
|
eccezione = false;
|
||||||
|
try {
|
||||||
|
input = Convert.ToDouble(Console.ReadLine());
|
||||||
|
}
|
||||||
|
catch (FormatException) {
|
||||||
|
Console.WriteLine("Numero non valido");
|
||||||
|
Pausa();
|
||||||
|
eccezione = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while (eccezione);
|
||||||
|
|
||||||
|
Console.WriteLine($"Il risultato della divisione è {Divisione(input)}");
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
array = Vettore();
|
||||||
|
for (int i = 0; i < array.Length; i++) {
|
||||||
|
Console.WriteLine($"Elemento n. {i}: {array[i]}");
|
||||||
|
}
|
||||||
|
Pausa();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
Console.WriteLine("Opzione non valida.");
|
||||||
|
Pausa();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while (eccezione || scelta != 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void Pausa() {
|
||||||
|
Console.WriteLine("Premere un tasto per continuare. . .");
|
||||||
|
Console.ReadKey();
|
||||||
|
}
|
||||||
|
|
||||||
|
static double Divisione(double input) {
|
||||||
|
Random r = new Random();
|
||||||
|
bool eccezione;
|
||||||
|
double ritorno = 0;
|
||||||
|
do {
|
||||||
|
eccezione = false;
|
||||||
|
try {
|
||||||
|
ritorno = input / r.Next(0, 4);
|
||||||
|
}
|
||||||
|
catch (DivideByZeroException) {
|
||||||
|
Console.WriteLine("Impossibile dividere per 0.");
|
||||||
|
Pausa();
|
||||||
|
eccezione = true;
|
||||||
|
}
|
||||||
|
} while (eccezione);
|
||||||
|
return ritorno;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int[] Vettore() {
|
||||||
|
int[] ritorno = new int[5];
|
||||||
|
int inputIndex = 0;
|
||||||
|
bool eccezione;
|
||||||
|
do {
|
||||||
|
Console.Write("In quale posizione inserire il numero? ");
|
||||||
|
eccezione = false;
|
||||||
|
try {
|
||||||
|
inputIndex = Convert.ToInt32(Console.ReadLine());
|
||||||
|
}
|
||||||
|
catch (FormatException) {
|
||||||
|
Console.WriteLine("Numero non valido.");
|
||||||
|
Pausa();
|
||||||
|
eccezione = true;
|
||||||
|
}
|
||||||
|
if (!eccezione) {
|
||||||
|
do {
|
||||||
|
Console.Write("Inserire un numero: ");
|
||||||
|
eccezione = false;
|
||||||
|
try {
|
||||||
|
ritorno[inputIndex] = Convert.ToInt32(Console.ReadLine());
|
||||||
|
}
|
||||||
|
catch (FormatException) {
|
||||||
|
Console.WriteLine("Numero non valido");
|
||||||
|
Pausa();
|
||||||
|
eccezione = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while (eccezione);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while (eccezione);
|
||||||
|
return ritorno;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,4 @@
|
|||||||
|
// <autogenerated />
|
||||||
|
using System;
|
||||||
|
using System.Reflection;
|
||||||
|
[assembly: global::System.Runtime.Versioning.TargetFrameworkAttribute(".NETCoreApp,Version=v9.0", FrameworkDisplayName = ".NET 9.0")]
|
||||||
22
obj/Debug/net9.0/eccezioni_1.AssemblyInfo.cs
Normal file
22
obj/Debug/net9.0/eccezioni_1.AssemblyInfo.cs
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
//------------------------------------------------------------------------------
|
||||||
|
// <auto-generated>
|
||||||
|
// This code was generated by a tool.
|
||||||
|
//
|
||||||
|
// Changes to this file may cause incorrect behavior and will be lost if
|
||||||
|
// the code is regenerated.
|
||||||
|
// </auto-generated>
|
||||||
|
//------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Reflection;
|
||||||
|
|
||||||
|
[assembly: System.Reflection.AssemblyCompanyAttribute("eccezioni_1")]
|
||||||
|
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
|
||||||
|
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
|
||||||
|
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+2c4f0d282678697de35d9d6f8a7a44d19dfc5599")]
|
||||||
|
[assembly: System.Reflection.AssemblyProductAttribute("eccezioni_1")]
|
||||||
|
[assembly: System.Reflection.AssemblyTitleAttribute("eccezioni_1")]
|
||||||
|
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
|
||||||
|
|
||||||
|
// Generated by the MSBuild WriteCodeFragment class.
|
||||||
|
|
||||||
1
obj/Debug/net9.0/eccezioni_1.AssemblyInfoInputs.cache
Normal file
1
obj/Debug/net9.0/eccezioni_1.AssemblyInfoInputs.cache
Normal file
@ -0,0 +1 @@
|
|||||||
|
a135b94a2973b38148c12c68634384a95bd847d487811137403326600055d751
|
||||||
@ -0,0 +1,15 @@
|
|||||||
|
is_global = true
|
||||||
|
build_property.TargetFramework = net9.0
|
||||||
|
build_property.TargetPlatformMinVersion =
|
||||||
|
build_property.UsingMicrosoftNETSdkWeb =
|
||||||
|
build_property.ProjectTypeGuids =
|
||||||
|
build_property.InvariantGlobalization =
|
||||||
|
build_property.PlatformNeutralAssembly =
|
||||||
|
build_property.EnforceExtendedAnalyzerRules =
|
||||||
|
build_property._SupportedPlatformList = Linux,macOS,Windows
|
||||||
|
build_property.RootNamespace = eccezioni_1
|
||||||
|
build_property.ProjectDir = /home/Verde/git/eccezioni_1/
|
||||||
|
build_property.EnableComHosting =
|
||||||
|
build_property.EnableGeneratedComInterfaceComImportInterop =
|
||||||
|
build_property.EffectiveAnalysisLevelStyle = 9.0
|
||||||
|
build_property.EnableCodeStyleSeverity =
|
||||||
8
obj/Debug/net9.0/eccezioni_1.GlobalUsings.g.cs
Normal file
8
obj/Debug/net9.0/eccezioni_1.GlobalUsings.g.cs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
// <auto-generated/>
|
||||||
|
global using global::System;
|
||||||
|
global using global::System.Collections.Generic;
|
||||||
|
global using global::System.IO;
|
||||||
|
global using global::System.Linq;
|
||||||
|
global using global::System.Net.Http;
|
||||||
|
global using global::System.Threading;
|
||||||
|
global using global::System.Threading.Tasks;
|
||||||
BIN
obj/Debug/net9.0/eccezioni_1.assets.cache
Normal file
BIN
obj/Debug/net9.0/eccezioni_1.assets.cache
Normal file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user