如何解决预期的捕获或最终 [英] How to solve expected catch or finaly
本文介绍了如何解决预期的捕获或最终的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码:
this my code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ExceptionHandling
{
class Program
{
static void Main(string[] args)
{
double bil1, bil2, hasil = 0;
try
{
Console.Write("Masukan Bilangan 1:");
bil1 = Convert.ToDouble(Console.ReadLine());
Console.Write("Masukan Bilangan 2:");
bil2 = Convert.ToDouble(Console.ReadLine());
hasil = bil1 / bil2;
Console.WriteLine("Hasilnya :" + hasil.ToString());
}
catch (FormatException fex)
{
Console.WriteLine("Error : Format Input tidak sesuai..");
}
catch (Exception ex)
{
Console.WriteLine("Error : " + ex.Message);
}
byte nilai = 0;
try
{
Console.Write("Masukan Nilai :");
nilai = Convert.ToByte(Console.ReadLine());
}
catch (OverflowException ofex)
{
Console.WriteLine("Error : Tipe data tidak cukup menampung nilai");
}
catch (Exception ex)
{
Console.WriteLine("Error :" + ex.Message);
Lingkaran objLing = new Lingkaran();
try
{
Console.Write("Masukan diameter :");
objLing.Diameter = Convert.ToDouble(Console.ReadLine());
}
}
}
}
}
What I have tried:
help me.. How to solve expected catch or finaly my code
推荐答案
如果输入不正确,请使用不会引发错误的转换运算符,而不是使用Try / Catch:
Instead of using Try/Catch, use the conversion operators that do not throw an error if the input is incorrect:
using System;
namespace YourConsoleApp
{
class Program
{
static void Main(string[] args)
{
int bill1;
int tryLimit = 3;
int tryCnt = 0;
Console.WriteLine("enter an integer");
while (tryCnt < tryLimit && !Int32.TryParse(Console.ReadLine(), out bill1))
{
tryCnt++;
// invalid input
if (tryCnt < tryLimit) Console.WriteLine("invalid input ... enter an integer");
}
if (tryCnt == tryLimit)
{
Console.WriteLine("too many incorrect entry attempts");
Console.Write("?");
Console.ReadLine();
// exit the app here ?
}
else
{
// you now have a valid integer in the variable 'bill1
}
}
}
}
你有一个尝试
块,它没有catch
块也不是finally
块:
You have atry
block at the end which has neither acatch
block nor afinally
block:
Quote:
catch (Exception ex)
{
Console.WriteLine("Error :" + ex.Message);
Lingkaran objLing = new Lingkaran();
try
{
Console.Write("Masukan diameter :");
objLing.Diameter = Convert.ToDouble(Console.ReadLine());
}
// No "catch" or "finally" block here...
}
要修复语法错误,请添加 catch
或最后
块,或删除尝试
块。
To fix the syntax error, either add a catch
or a finally
block, or remove the try
block.
catch (Exception ex)
{
Console.WriteLine("Error :" + ex.Message);
Lingkaran objLing = new Lingkaran();
Console.Write("Masukan diameter :");
objLing.Diameter = Convert.ToDouble(Console.ReadLine());
}
但正如比尔所说,最好使用 Double.TryParse
尝试将字符串转换为数字,而不是使用 Convert.ToDouble
并捕获异常。
Double.TryParse方法(系统)| Microsoft Docs [ ^ ]
But as Bill said, it's better to use Double.TryParse
to try to convert a string to a number, rather than using Convert.ToDouble
and catching the exception.
Double.TryParse Method (System) | Microsoft Docs[^]
当异常类型未知时,它期望一个通用的catch或者最后的结尾。错误意味着它所说的:)
It's expecting a generic catch or a finally at the end, for when the type of exception is unknown. The error means what it says :)
这篇关于如何解决预期的捕获或最终的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文