为什么不能以下列方式抛出InterruptedException:
try {
System.in.wait(5) //Just an example
} catch (InterruptedException exception) {
exception.printStackTrace();
//On this next line I am confused as to why it will not let me throw the exception
throw exception;
}
Run Code Online (Sandbox Code Playgroud)
我去了http://java24hours.com,但它没有告诉我为什么我不能抛出InterruptedException.
如果有人知道为什么,请告诉我!我很绝望!:S
class A
{
public:
virtual ~A()
{
}
};
class B : virtual public A
{
public:
~B() throw()
{}
};
class C : public B
{
};
int main(int argc, char * argv [])
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
该代码给出以下错误:
error: looser throw specifier for ‘virtual C::~C()’
error: overriding ‘virtual B::~B() throw ()’
Run Code Online (Sandbox Code Playgroud)
在我的debian测试中(gcc(Debian 4.6.0-10)4.6.1 20110526(预发布))但在先前的gcc版本上编译没有错误(我的debian系统再次4.5).
异常规范如何影响虚拟析构函数覆盖? 根据该答案,编译器应该创建一个与基类的throw声明匹配的默认构造函数.显然这不是新gcc上发生的事情.什么是改变,什么是正确的编译器行为,除了在派生类中手动添加空析构函数(例如编译器标志)之外,还有一些简单的解决方案.
namespace QuantLib {
//! Base error class
class Error : public std::exception {
public:
/*! The explicit use of this constructor is not advised.
Use the QL_FAIL macro instead.
*/
Error(const std::string& file,
long line,
const std::string& functionName,
const std::string& message = "");
/*! the automatically generated destructor would
not have the throw specifier.
*/
~Error() throw() {}
//! returns the error message.
const char* what() const throw ();
private:
boost::shared_ptr<std::string> message_;
};
}
Run Code Online (Sandbox Code Playgroud)
正如您在注释中看到的那样,类的析构函数Error显式提供了一个带有no-throw说明符的空实现.
问题:这有必要吗?或者这是一个很好的做法,比较让编译器生成隐式析构函数?
可以抛出也可以用来退出switch语句而不使用break关键字吗?为什么要使用投掷而不是休息?
switch(number)
{
case 1:
throw new RuntimeException("Exception number 1");
case 2:
throw new RuntimeException("Exception number 2");
}
Run Code Online (Sandbox Code Playgroud) 做代码分析给了我CA2200项:
CA2200 Rethrow保留堆栈详细信息'func()'重新捕获捕获的异常并将其明确指定为参数.使用不带参数的'throw',以保留最初引发异常的堆栈位置.
我已经实现了这个建议,但无论如何我似乎都得到了相同的堆栈跟踪.
这是我的测试代码和输出(空格用于给出明显的行号):
第30行的预期错误
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace throw_test
{
class Program
{
static void Main(string[] args)
{
try
{
try
{
// line 22 below
throw new Exception();
}
catch (Exception ex) // DEFINES AND THROWS 'ex'
{
// line 30 below
throw ex;
}
}
catch (Exception ex)
{
Console.Write(ex.StackTrace);
}
Console.Read();
}
}
}
Run Code Online (Sandbox Code Playgroud)
产量
在c:\ Users\Richard\Documents\Visual Studio 2013\Projects\using_throw_closestream\using_throw_closestream\Program.cs中的throw_test.Program.Main(String [] args):第30行
第22行的预期错误
using System;
using …Run Code Online (Sandbox Code Playgroud) 您显然能够在不丢弃.NET中的堆栈跟踪的情况下重新抛出异常.
但它似乎没有起作用.
因此我遵循的基本用法是:
[WebMethod]
public void ExceptionTest()
{
try
{
throw new Exception("An Error Happened");
}
catch (Exception ex)
{
evlWebServiceLog.WriteEntry(ex.ToString(), EventLogEntryType.Error);
throw;
}
}
Run Code Online (Sandbox Code Playgroud)
问题是,行中的行中的throw;行号,而不是原始throw new行.
我在一个简单的exe项目中测试了它,没有登录到Windows日志行.它没有任何区别,堆栈跟踪总是包含错误的行号,使其不太有用.
它为什么这样做?我该怎么做?
任何人都能用一个例子清楚地说明Java异常处理中throw和throws之间的区别吗?我试过谷歌搜索但无法得出结论.请帮忙
我什么时候应该使用Exception,InvalidArgumentException或UnexpectedValueException?
我不知道他们之间真正的不同,因为我总是使用Exception.
我在C++中有一个shared_ptr,我想把它作为例外抛出.我正在编写一个测试用例,在gcc 6.4.0中使用了相同的情况并且卡住了.
我不明白为什么在处理异常后x为NULL,即使它没有超出范围或被分配给.
#include "stdafx.h"
#include <iostream>
#include <memory>
#include <string>
using namespace std;
class S {
private:
string s;
public:
string String()
{
return s;
}
S(string a) : s(a)
{
cout << "S::S( " << a << " )" << endl;
}
~S()
{
cout << "S::~S( " << String() << " )" << endl;
}
};
int main(int argc, char* argv[])
{
shared_ptr< S > x = make_shared<S>("hello world");
cout << "Before throw" << endl;
try { …Run Code Online (Sandbox Code Playgroud) 我对使用 Flutter 进行编码非常陌生,并且遵循本课程可以让我创建有状态的小部件。问题是当我这样做时,我得到这个 throw UnimplementedError(); 它应该返回 null。我不知道我做错了什么。
my code:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
State<StatefulWidget> createState() {
// TODO: implement createState
throw UnimplementedError();
}
}
class MyAppState extends State<MyApp> {
var questionIndex = 0;
void answerQuestion() {
setState(() {
questionIndex = questionIndex + 1;
});
print(questionIndex);
}
@override
Widget build(BuildContext context) {
var questions = [
"What's your favourite color?",
"What's your favourite animal?",
];
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text( …Run Code Online (Sandbox Code Playgroud)