我有一些像这样的代码:
try
{
doStuff();
}
catch(SpecificException)
{
if(e.Message == interestingMessage)
doOtherStuff();
else
throw;
}
catch(Exception e)
{
doSomethingElse();
}
Run Code Online (Sandbox Code Playgroud)
当第一个catch块重新抛出异常时,它会被第二个catch块捕获吗?
我们在代码中使用try catch块。我想问的是,使用finally块是一种好习惯。我还没看到多少最终会阻塞代码。这是不好的做法吗?
好的,所以我是错误处理的新手,我已经看到了一些例子,但我还没有看到这个问题的答案.我将使用一些真正的基本示例代码来显示我在问什么.
if(some condition){
throw Exception()
}
//Some random code in between
echo "Code between throw() and Catch()";
catch(Exception $e){
//handle the caught exception
}
Run Code Online (Sandbox Code Playgroud)
所以基本上,我的问题是 - 如果if()中的条件导致抛出异常,随机echo语句会执行,还是会跳过并直接进入异常的catch()?
我正在尝试为学校作业制作一个功能齐全的计算器应用程序.要做到这一点,虽然我需要使用try-catch来处理DivideByZero错误,这是我现在的代码:
try
{
Console.WriteLine("Type 1st number: ");
num1 = Convert.ToInt32(Console.ReadLine());
Console.Write("type 2nd number: ");
num2 = Convert.ToInt32(Console.ReadLine());
Console.Write("type operation( x , / , +, -, Fs) ");
operation = Console.ReadLine();
}
catch(DivideByZeroException)
{
Console.WriteLine("Sorry moron you can't divide by zero");
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用.它似乎使我的代码在顶部无效.有人知道如何正确格式化吗?
PS完整代码在这里:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Calculator_MK._2
{
class Program
{
static void Main(string[] args)
{
Calculator();
}
private static void Calculator()
{
decimal num1;
decimal num2;
string operation;
decimal result;
decimal …
Run Code Online (Sandbox Code Playgroud) 我的编译器给出了以下代码的错误:
#include <iostream>
#include <stdexcept>
using namespace std;
void test()
{
throw runtime_error("Error");
}
int main()
{
try
{
test();
}
for (int i = 0; i < 10; i++)
{
}
catch (exception& e)
{
cout << e.what();
}
}
Run Code Online (Sandbox Code Playgroud)
它说"错误:预期'捕获'之前'('令牌',它指的是for循环初始化中的'(').
在try块之后我是否必须立即编写catch块?我认为如果在try块中抛出一个错误,程序将冒出来,直到它找到一个合适的catch块.为什么这不适用于此?
我有这个数组:
const arr = [
{ 'Some text': this.f('a') },
{ 'Some other text': this.f('b') }
]
Run Code Online (Sandbox Code Playgroud)
为了实现这一目标,我编写了实验代码。
arr.forEach((entry) => {
console.log(Object.entries(entry));
})
Run Code Online (Sandbox Code Playgroud)
结果它执行了我的功能:
[[ 'Some text': 'result_f1' ]]
[[ 'Some other text': 'result_f2' ]]
Run Code Online (Sandbox Code Playgroud)
我的函数自动执行了。
但我不认为这是实现这一目标的最安全方法。我想更明确地分别执行每个函数并尝试捕获每个函数。
我想这样做的原因是将每个函数包装到一个单独的try-catch
块中,因为在数组中进行尝试捕获会破坏代码的可读性
有什么想法如何实现这一目标吗?
我在发布配置中使用VS2010和C++
以下执行正常:
int status;
try
{
status = myfunction(arg1, arg2);
}
catch (int e)
{
cout << "An exception occurred. Exception Nr. " << e << endl;
}
Run Code Online (Sandbox Code Playgroud)
但是,以下程序崩溃了:
int status;
status = myfunction(arg1, arg2);
Run Code Online (Sandbox Code Playgroud)
发生了什么?
我没有方法的来源,myfunction,这是第三方dll的一部分.
我正在编写一个 Java 文件,但是当我尝试编译它时,我不断收到 3 条错误消息:
reader.java:35: 错误:'try' 没有 'catch'、'finally' 或资源声明 try
reader.java:48: 错误:'catch' 没有 'try' catch(IOException e)
reader.java:52: 错误: 'catch' 没有 'try' catch(Exception e)
3 错误
为什么?我错过了什么?这是我的代码:
public static void processRecords(String filenameIn, Person[] personArray)
{
try
{
FileReader fr = new FileReader(filenameIn);
BufferedReader reader = new BufferedReader(fr);
reader.readLine();
for (int i = 0; i <personArray.length; i++)
{
String[] data = reader.readLine().split("/t");
personArray[i] = new Person(Integer.parseInt(data[0]), data[1], data[2], Integer.parseInt(data[3]));
}
}
reader.close();
catch(IOException e)
{
System.out.println("ERROR: WRONG FILE " + e.toString()); …
Run Code Online (Sandbox Code Playgroud) 当我运行程序时,100除以数字,但一旦达到0,就会出现一条消息告诉用户100不能除以0.
如何在错误发生后让程序继续运行,并将100除以其余数字.
这是我目前的代码:
public class testing {
int[] table;
int number;
public testing(int number) {
this.number = number;
table = new int[number];
}
public static void main(String[] args) {
try {
int[] numbers = { 2, 3, 0, 4, 5 };
testing division;
for (int i = 0; i < 5; i++) {
division = new testing(100 / numbers[i]);
System.out.println("Answer is " + division.number);
}
} catch (Exception e) {
System.out.println("A number cannot be divided by 0");
}
}
}
Run Code Online (Sandbox Code Playgroud) 我有很多自定义异常,我在代码中的特定情况下抛出,我想在方法的底部有一个catch块来处理它们.
所有异常都是Exception类CribbageException的子代,所以我想要:
public void myMethod(){
if (whatever){
throw new CardException();
}
if (something else){
throw new InvalidCardException();
}
if (scenario 3){
throw new TwoCardsException();
}
catch (CribbageException e) {
System.out.println(e.getMessage());
}
}
Run Code Online (Sandbox Code Playgroud)
但我得到一个没有尝试错误的捕获.
有没有办法使用这种类型的异常处理?
#setBirthday sets their Birthday to a date
def setBirthday(self):
while True:
try:
day = int(raw_input('Please enter the date of the month (1-31) you were born on here ->'))
if day <= 0 or day >= 32:
print "better try again..."
except ValueError:
continue
else:
break
month = int(raw_input('Please enter the month of the year (1-12) you were born on here ->'))
if month <= 0 or day >= 13:
print "better try again..."
except ValueError:
continue
else:
break
year = …
Run Code Online (Sandbox Code Playgroud)